package main import ( "crypto/md5" "encoding/hex" "fmt" "git.wanpinghui.com/WPH/go_common/wph" "git.wanpinghui.com/WPH/go_common/wph/date" "net/url" "strings" ) func GetLiveSingedURL(scheme, host, app, stream, key string, expire date.Datetime, args url.Values) string { scheme = strings.TrimSpace(scheme) host = strings.TrimSpace(host) app = strings.TrimSpace(app) stream = strings.TrimSpace(stream) key = strings.TrimSpace(key) if scheme == "" { scheme = "rtmp" } if host == "" || app == "" || stream == "" || key == "" || expire.IsZero() { return "" } nonce := wph.NextId() // 随机码 path := fmt.Sprintf("/%s/%s", app, stream) // /AppName/StreamName及后缀 paramStr := fmt.Sprintf("%d-%d-0", expire.Unix(), nonce) // 地址过期时间UNIX时间戳-随机码-0 origin := fmt.Sprintf("%s-%s-%s", path, paramStr, key) // 待签名字符串 md5hash := md5sum(origin) result := fmt.Sprintf("%s://%s%s?auth_key=%s-%s", scheme, host, path, paramStr, md5hash) // 结果URL if nil != args && len(args) > 0 { result = fmt.Sprintf("%s&%s", result, args.Encode()) } return result } func md5sum(str string) string { h := md5.New() h.Write([]byte(str)) return hex.EncodeToString(h.Sum(nil)) } var DefaultAudioArgs url.Values func init() { DefaultAudioArgs = url.Values{} DefaultAudioArgs.Add("onlyaudio", "1") } // 测试 func main() { expire := date.Now().AddDays(1) scheme := `rtmp` appName := `shy-live-test` // 以下参数正常应由配置读取 pushHostName := `livepush-appmedia.zlzgy.org.cn` pullHostName := `live-appmedia.zlzgy.org.cn` pushKey := `N3IC5OfIBg` pullKey := `utPuNSNngo` // 视频直播 videoStreamName := wph.NextId().String() videoPushUrl := GetLiveSingedURL(scheme, pushHostName, appName, videoStreamName, pushKey, expire, nil) println(`video push url:`, videoPushUrl) videoPullUrl := GetLiveSingedURL(scheme, pullHostName, appName, videoStreamName, pullKey, expire, nil) println(`video pull url:`, videoPullUrl) // 音频直播 audioStreamName := wph.NextId().String() audioPushUrl := GetLiveSingedURL(scheme, pushHostName, appName, audioStreamName, pushKey, expire, DefaultAudioArgs) println(`audio push url:`, audioPushUrl) audioPullUrl := GetLiveSingedURL(scheme, pullHostName, appName, audioStreamName, pullKey, expire, DefaultAudioArgs) println(`audio pull url:`, audioPullUrl) }