Skip to content

Commit

Permalink
relay_ip 指定
Browse files Browse the repository at this point in the history
  • Loading branch information
danbai225 committed Dec 5, 2023
1 parent 74be8a0 commit d324fd0
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 18 deletions.
1 change: 1 addition & 0 deletions README-English.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ relay - relay service to be used in case of impenetration
- server_port Server start port Only server configuration
- reg_port The port where the relay is registered to listen when the server is started.
- must_key key is required to Connect
- relay_ip ip of RelayService onlyRelayConfiguration

# docker-compose installation

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ relay - 在无法穿透情况下使用的中继服务
- server_port 服务端启动端口 仅server配置
- reg_port 服务端启动的relay注册监听端口 仅server配置
- must_key 必须带key才能连接
- relay_ip 中继服务的ip 仅relay配置

# docker-compose安装

下载仓库中的`docker-compose.yml``config.json`
Expand Down
1 change: 1 addition & 0 deletions common/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Config struct {
ServerPort uint16 `json:"server_port"`
RelayPort uint16 `json:"relay_port"`
MustKey bool `json:"must_key"`
RelayIp string `json:"relay_ip"`
}

var Conf *Config
Expand Down
6 changes: 3 additions & 3 deletions config.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"whiteList": false,
"ipList": [
],
"ipList": [],
"must_key": true,
"debug": false,
"reg_server":"127.0.0.1:21120",
"relay_name":"go_rust_desk_relay",
"server_port": 21116,
"relay_port":21117,
"reg_port": 21120
"reg_port": 21120,
"relay_ip": "127.0.0.1"
}
1 change: 1 addition & 0 deletions model/model_msg/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type RegMsg struct {
Ping uint `json:"ping"`
Cpu uint `json:"cpu"`
NetFlow float64 `json:"net_flow"`
IP string `json:"ip"`
}
type RegMsgR struct {
Err string `json:"err"`
Expand Down
4 changes: 2 additions & 2 deletions relay/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func handlerMsg(msg []byte, writer *common.Writer) {
}
}

//黑名单检测
// 黑名单检测
func blacklistDetection(addr *common.Addr) bool {
in := common.InList(addr.GetIP())
if common.Conf.WhiteList && in {
Expand Down Expand Up @@ -92,7 +92,7 @@ func regRelay() {
Base: model_msg.Base{
MsgType: model_msg.RegType,
},
RegMsg: &model_msg.RegMsg{Name: common.Conf.RelayName, Time: time.Now(), RelayPort: common.Conf.RelayPort, Upload: u, Download: d, Ping: p, Cpu: cpuTest(), NetFlow: netFlow()},
RegMsg: &model_msg.RegMsg{IP: common.Conf.RelayIp, Name: common.Conf.RelayName, Time: time.Now(), RelayPort: common.Conf.RelayPort, Upload: u, Download: d, Ping: p, Cpu: cpuTest(), NetFlow: netFlow()},
})
_, err1 := dial.Write(marshal)
if err1 != nil {
Expand Down
16 changes: 7 additions & 9 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package server
import (
"fmt"
logs "github.com/danbai225/go-logs"
"github.com/gogf/gf/v2/container/gqueue"
"github.com/gogf/gf/v2/container/gring"
"go-rustdesk-server/common"
"go-rustdesk-server/data_server"
Expand All @@ -12,7 +11,6 @@ import (
)

var dataSever data_server.DataSever
var queue = gqueue.New()
var r = gring.New(32, true)
var rendezvousServers = make([]string, 0)
var serial = int32(1)
Expand All @@ -32,7 +30,7 @@ func Start() {
common.NewMonitor(false, "tcp", fmt.Sprintf(":%d", common.Conf.ServerPort), handlerMsg).Start()
}

//黑名单检测
// 黑名单检测
func blacklistDetection(id string, addr *common.Addr) bool {
in := common.InList(addr.GetIP())
if common.Conf.WhiteList && in {
Expand All @@ -53,7 +51,7 @@ func loadRelay() {
}
}

//获取一个中继服务器
// 获取一个中继服务器
func getRelay() string {
online, _ := dataSever.GetRelayAllOnline()
if len(online) != len(rendezvousServers) {
Expand All @@ -63,15 +61,15 @@ func getRelay() string {
logs.Err("NoRegisteredRelayServer")
return ""
}
var Rrelay *model.Relay
var Relay *model.Relay
for _, relay := range online {
if relay.Cpu < 60 && (float64(relay.Upload)-relay.NetFlow) > (float64(relay.Upload)*0.1) {
Rrelay = relay
Relay = relay
break
}
}
if Rrelay == nil {
Rrelay = online[rand.Intn(len(online))]
if Relay == nil {
Relay = online[rand.Intn(len(online))]
}
return fmt.Sprintf("%s:%d", Rrelay.IP, Rrelay.Port)
return fmt.Sprintf("%s:%d", Relay.IP, Relay.Port)
}
12 changes: 8 additions & 4 deletions server/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"go-rustdesk-server/model/model_msg"
)

//同步服务
// 同步服务
func handlerSyncMsg(msg []byte, writer *common.Writer) {
if len(msg) == 0 {
return
Expand All @@ -33,7 +33,7 @@ func handlerSyncMsg(msg []byte, writer *common.Writer) {
}
}

//regRelay
// regRelay
func regRelay(msg *model_msg.RegMsg, writer *common.Writer) {
m := model_msg.Msg{
Base: model_msg.Base{MsgType: model_msg.RegRType},
Expand All @@ -52,10 +52,14 @@ func regRelay(msg *model_msg.RegMsg, writer *common.Writer) {
m.RegMsgR.Err = err.Error()
return
}
ip := writer.GetAddr().GetIP()
if msg.IP != "" {
ip = msg.IP
}
newRelay := &model.Relay{
Name: msg.Name,
Port: msg.RelayPort,
IP: writer.GetAddr().GetIP(),
IP: ip,
Online: true,
LastRegTime: &msg.Time,
Download: msg.Download,
Expand All @@ -78,7 +82,7 @@ func regRelay(msg *model_msg.RegMsg, writer *common.Writer) {
}})
loadRelay()
logs.Info("new relay", newRelay.Name, writer.GetAddrStr())
} else if relay.IP == writer.GetAddr().GetIP() {
} else if relay.IP == ip {
newRelay.Uid = relay.Uid
err = dataSever.UpdateRelay(newRelay)
if err != nil {
Expand Down

0 comments on commit d324fd0

Please sign in to comment.