Skip to content

Commit

Permalink
增加随机端口范围
Browse files Browse the repository at this point in the history
  • Loading branch information
langhuihui committed Dec 7, 2020
1 parent c284e4e commit 3ffb586
Show file tree
Hide file tree
Showing 11 changed files with 2,655 additions and 2,545 deletions.
41 changes: 26 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gb28181

import (
"log"
"math/rand"
"net"
"net/http"
"strconv"
Expand All @@ -18,12 +19,14 @@ import (

var Devices sync.Map
var config = struct {
Serial string
Realm string
ListenAddr string
Expires int
AutoInvite bool
}{"34020000002000000001", "3402000000", "127.0.0.1:5060", 3600, true}
Serial string
Realm string
ListenAddr string
Expires int
AutoInvite bool
MediaPortMin uint16
MediaPortMax uint16
}{"34020000002000000001", "3402000000", "127.0.0.1:5060", 3600, true, 58200, 58300}

func init() {
InstallPlugin(&PluginConfig{
Expand Down Expand Up @@ -55,8 +58,8 @@ func run() {

AudioEnable: true,
WaitKeyFrame: true,
MediaPortMin: 58200,
MediaPortMax: 58300,
MediaPortMin: config.MediaPortMin,
MediaPortMax: config.MediaPortMax,
MediaIdleTimeout: 30,
}
s := transaction.NewCore(config)
Expand Down Expand Up @@ -119,7 +122,6 @@ func run() {
})
s.Start()
}

func onPublish(channel *transaction.Channel) (port int) {
rtpPublisher := new(rtp.RTP_PS)
if !rtpPublisher.Publish("gb28181/" + channel.DeviceID) {
Expand All @@ -131,19 +133,28 @@ func onPublish(channel *transaction.Channel) (port int) {
}
}()
rtpPublisher.Type = "GB28181"
addr, err := net.ResolveUDPAddr("udp", ":0")
if err != nil {
return
var conn *net.UDPConn
var err error
rang := int(config.MediaPortMax - config.MediaPortMin)
for count := rang; count > 0; count-- {
randNum := rand.Intn(rang)
port = int(config.MediaPortMin) + randNum
addr, _ := net.ResolveUDPAddr("udp", ":"+strconv.Itoa(port))
conn, err = net.ListenUDP("udp", addr)
if err != nil {
continue
} else {
break
}
}
conn, err := net.ListenUDP("udp", addr)
if err != nil {
return
}
networkBuffer := 1048576
if err := conn.SetReadBuffer(networkBuffer); err != nil {
if err = conn.SetReadBuffer(networkBuffer); err != nil {
Printf("udp server video conn set read buffer error, %v", err)
}
if err := conn.SetWriteBuffer(networkBuffer); err != nil {
if err = conn.SetWriteBuffer(networkBuffer); err != nil {
Printf("udp server video conn set write buffer error, %v", err)
}
la := conn.LocalAddr().String()
Expand Down
2 changes: 1 addition & 1 deletion transaction/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ func (c *Core) AddDevice(msg *sip.Message) *Device {
core: c,
from: &sip.Contact{Uri: msg.StartLine.Uri, Params: make(map[string]string)},
to: msg.To,
addr: msg.Via.GetSendBy(),
Addr: msg.Via.GetSendBy(),
}
c.Devices.Store(msg.From.Uri.UserInfo(), v)
return v
Expand Down
4 changes: 2 additions & 2 deletions transaction/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type Device struct {
sn int
from *sip.Contact
to *sip.Contact
addr string
Addr string
}

func (c *Core) RemoveDead() {
Expand Down Expand Up @@ -100,7 +100,7 @@ func (d *Device) CreateMessage(Method sip.Method) (requestMsg *sip.Message) {
ID: 1,
Method: Method,
}, CallID: utils.RandNumString(10),
Addr: d.addr,
Addr: d.Addr,
}
requestMsg.From.Params["tag"] = utils.RandNumString(9)
return
Expand Down
2,435 changes: 1,233 additions & 1,202 deletions ui/dist/plugin-gb28181.common.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion ui/dist/plugin-gb28181.common.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion ui/dist/plugin-gb28181.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 3ffb586

Please sign in to comment.