2 Star 0 Fork 0

slh92/plugin-sip

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
common.go 3.83 KB
一键复制 编辑 原始数据 按行查看 历史
package tu
import (
"fmt"
"gitee.com/slh1992/plugin-sip/sip"
"gitee.com/slh1992/plugin-sip/utils"
"strings"
"time"
)
//根据参数构建各种消息
//参数来自于session/transaction等会话管理器
/*
method:请求方法
transport:UDP/TCP
sipSerial: sip server ID
sipRealm: sip domain
username: 用户名/设备序列号
srcIP: 源IP
srcPort:源端口
expires: 过期时间
cseq:消息序列号,当前对话递增
*/
//构建消息:以客户端(可能是IPC,也可能是SIP Server)的角度
func BuildMessageRequest(method sip.Method, transport, sipSerial, sipRealm, username, srcIP string, srcPort uint16, expires, cseq int, body string) *sip.Message {
server := fmt.Sprintf("%s@%s", sipSerial, sipRealm)
client := fmt.Sprintf("%s@%s", username, sipRealm)
msg := &sip.Message{
Mode: sip.SIP_MESSAGE_REQUEST,
MaxForwards: 70,
UserAgent: "IPC",
Expires: expires,
ContentLength: 0,
}
msg.StartLine = &sip.StartLine{
Method: method,
Uri: sip.NewURI(server),
}
msg.Via = &sip.Via{
Transport: transport,
Host: fmt.Sprintf("%s:%d", srcIP, srcPort),
}
msg.Via.Params = map[string]string{
"branch": randBranch(),
"rport": "-1", //only key,no-value
}
msg.From = &sip.Contact{
Uri: sip.NewURI(client),
Params: nil,
}
msg.From.Params = map[string]string{
"tag": utils.RandNumString(10),
}
msg.To = &sip.Contact{
Uri: sip.NewURI(client),
}
msg.CallID = utils.RandNumString(8)
msg.CSeq = &sip.CSeq{
ID: uint32(cseq),
Method: method,
}
msg.Contact = &sip.Contact{
Uri: sip.NewURI(fmt.Sprintf("%s@%s:%d", username, srcIP, srcPort)),
}
if len(body) > 0 {
msg.ContentLength = len(body)
msg.Body = body
}
msg.Date = time.Now()
return msg
}
//根据参数构建各种消息
//参数来自于session/transaction等会话管理器
/*
method:请求方法
transport:UDP/TCP
sipSerial: sip server ID
sipRealm: sip domain
username: 用户名/设备序列号
srcIP: 源IP
srcPort:源端口
expires: 过期时间
cseq:消息序列号,当前对话递增
*/
//构建消息:以客户端(可能是IPC,也可能是SIP Server)的角度
func BuildClientMessageRequest(method sip.Method, transport, sipSerial, sipRealm, username, srcIP string, srcPort uint16, expires, cseq int, body string) *sip.Message {
server := fmt.Sprintf("%s@%s", sipSerial, sipRealm)
client := fmt.Sprintf("%s@%s", username, sipRealm)
msg := &sip.Message{
Mode: sip.SIP_MESSAGE_REQUEST,
MaxForwards: 70,
UserAgent: "IPC",
Expires: expires,
ContentLength: 0,
}
msg.StartLine = &sip.StartLine{
Method: method,
Uri: sip.NewURI(server),
}
msg.Via = &sip.Via{
Transport: transport,
Host: fmt.Sprintf("%s:%d", srcIP, srcPort),
}
msg.Via.Params = map[string]string{
"branch": randBranch(),
"rport": "-1", //only key,no-value
}
msg.From = &sip.Contact{
Uri: sip.NewURI(client),
Params: nil,
}
msg.From.Params = map[string]string{
"tag": utils.RandNumString(10),
}
msg.To = &sip.Contact{
Uri: sip.NewURI(client),
}
msg.CallID = utils.RandNumString(8)
msg.CSeq = &sip.CSeq{
ID: uint32(cseq),
Method: method,
}
msg.Contact = &sip.Contact{
Uri: sip.NewURI(fmt.Sprintf("%s@%s:%d", username, srcIP, srcPort)),
}
if len(body) > 0 {
msg.ContentLength = len(body)
msg.Body = body
}
msg.Date = time.Now()
return msg
}
//z9hG4bK + 10个随机数字
func randBranch() string {
return fmt.Sprintf("z9hG4bK%s", utils.RandNumString(8))
}
func ParseSDPAttr(data, key string) string {
data = strings.Trim(data, sip.CRLF)
msgArr := strings.Split(data, sip.CRLF)
if len(msgArr) == 0 {
return ""
}
for _, line := range msgArr {
pos := strings.IndexByte(line, '=')
if pos == -1 {
continue
}
k := strings.ToLower(strings.TrimSpace(line[:pos]))
v := strings.TrimSpace(line[pos+1:])
if k == key {
return v
}
}
return ""
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/slh1992/plugin-sip.git
git@gitee.com:slh1992/plugin-sip.git
slh1992
plugin-sip
plugin-sip
v1.3.7

搜索帮助