Commit 76aca1d4 authored by wei.xuan's avatar wei.xuan

Merge branch '1107' into 'master'

feat:add ip

See merge request !8
parents cfa24f9d 319274ed
......@@ -106,7 +106,7 @@ func (client *Client) handleUpStream(conn *net.TCPConn, packet *protocol.MajoraP
buf := make([]byte, common.BufSize) // 4k
cnt, err := conn.Read(buf)
if err != nil {
getty.GetLogger().Errorf("[handleUpStream] %d->read with error:%+v,l:%s->r:%s",
getty.GetLogger().Warnf("[handleUpStream] %d->read with error:%+v,l:%s->r:%s",
packet.SerialNumber, err, conn.LocalAddr(), conn.RemoteAddr())
client.OnClose(session, conn, packet.SerialNumber)
break
......@@ -116,7 +116,7 @@ func (client *Client) handleUpStream(conn *net.TCPConn, packet *protocol.MajoraP
pack.SerialNumber = packet.SerialNumber
if _, _, err := session.WritePkg(pack, common.SessionTimeout); err != nil {
getty.GetLogger().Debugf("[handleUpStream] %d-> write to server fail %+v", packet.SerialNumber, err.Error())
getty.GetLogger().Errorf("[handleUpStream] %d-> write to server fail %+v", packet.SerialNumber, err.Error())
client.OnClose(session, conn, packet.SerialNumber)
break
} else {
......
......@@ -2,6 +2,7 @@ package client
import (
"fmt"
"math/rand"
"net"
"time"
......@@ -68,8 +69,12 @@ func InitialSession(session getty.Session, client *Client) (err error) {
session.SetWaitTime(common.WaitTimeout)
if client.config.Redial.Valid() {
getty.GetLogger().Infof("ReconnInterval %+v", client.config.Redial.RedialDuration)
session.SetCronPeriod(int(client.config.Redial.RedialDuration.Milliseconds()))
rand.Seed(time.Now().UnixNano())
// 加上随机 防止vps在同时间重启
randDuration := rand.Int63n(time.Minute.Milliseconds() * 5)
interval := randDuration + client.config.Redial.RedialDuration.Milliseconds()
getty.GetLogger().Infof("ReconnInterval %+v", interval)
session.SetCronPeriod(int(interval))
}
session.SetPkgHandler(PkgCodec)
session.SetEventListener(&MajoraEventListener{
......
......@@ -50,7 +50,7 @@ const (
WaitTimeout = time.Minute
KeepAliveTimeout = time.Second * 10
SessionTimeout = time.Minute
SessionTimeout = time.Minute * 5
UpstreamTimeout = time.Minute
)
......@@ -72,7 +72,7 @@ const (
var (
ErrInvalidSize = errors.New("invalid size")
ErrInvalidMagic = errors.New("invalid magic")
ErrNilPacket = errors.New("nil packet")
ErrNilPacket = errors.New("nil packet")
)
func ConvertInt32ToBytes(input int32) []byte {
......
tunnel_addr = 127.0.0.1:5879
;tunnel_addr = aoba.vip:5879
;tunnel_addr = 127.0.0.1:5879
tunnel_addr = aoba.vip:5879
dns_server = 114.114.114.114:53
;bind to local ip
;local_ip = 192.168.0.100
......
......@@ -10,6 +10,7 @@ rm -fr start.sh
rm -fr majora.ini
rm -fr majora.service
rm -fr majora-dev.ini
rm -fr majora.log
mv -f majora-cli*/* .
......@@ -20,6 +21,10 @@ ln -s /root/majora.service /usr/lib/systemd/system/majora.service
systemctl daemon-reload
echo "start on reboot ..."
systemctl enable majora.service
echo "restart..."
systemctl restart majora.service
......
......@@ -15,7 +15,7 @@ var (
)
const (
defTimeout = time.Second * 10
defTimeout = time.Second * 5
baiduUrl = "https://www.baidu.com"
)
......@@ -23,6 +23,7 @@ const (
func init() {
httpcli = &http.Client{
Transport: &http.Transport{
TLSHandshakeTimeout: defTimeout,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
......
package infra
import (
"net"
"github.com/adamweixuan/getty"
)
const (
netname = "ppp0"
)
func GetPPP() string {
return GetIpByName(netname)
}
func GetIpByName(netname string) string {
ni, err := net.InterfaceByName(netname)
if err != nil {
getty.GetLogger().Warnf("get %s ip error %s", netname, err)
return ""
}
addrs, err := ni.Addrs()
if err != nil {
getty.GetLogger().Warnf("get ip addr err %s", err)
return ""
}
if len(addrs) == 0 {
getty.GetLogger().Warnf("get ip addr empty ")
return ""
}
var ipv4Addr net.IP
for _, addr := range addrs {
if ipv4Addr = addr.(*net.IPNet).IP.To4(); ipv4Addr != nil {
break
}
}
if ipv4Addr == nil {
getty.GetLogger().Warnf("interface %s don't have an ipv4 address", netname)
return ""
}
return ipv4Addr.String()
}
package infra
import (
"testing"
)
func TestGetIpByName(t *testing.T) {
type args struct {
netname string
}
tests := []struct {
name string
args args
want string
}{
{
name: "en0",
args: args{netname: "en0"},
want: "192.168.0.101",
},
{
name: "lo0",
args: args{netname: "lo0"},
want: "127.0.0.1",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := GetIpByName(tt.args.netname); got != tt.want {
t.Errorf("GetIpByName() = %v, want %v", got, tt.want)
}
})
}
}
......@@ -15,24 +15,26 @@ const (
)
func Redial(cfg *model.Configure, session getty.Session) {
getty.GetLogger().Warnf("[redial] start, session status:%s", session.Stat())
status := command(cfg)
getty.GetLogger().Warnf("[redial] end:%v, session status: %s", status, session.Stat())
pingBaidu := PingBaidu()
getty.GetLogger().Warnf("[redial] net check: %v", pingBaidu)
if !pingBaidu {
getty.GetLogger().Warnf("[redial] net check fail,redial...")
Redial(cfg, session)
getty.GetLogger().Warnf("[redial] start, session is close :%d", session.IsClosed())
beforeIp := GetPPP()
retry := 0
defer func(start time.Time) {
newIp := GetPPP()
getty.GetLogger().Warnf("[redial] retry %d, cost %v, ip change %s -> %s, session is close:%v",
retry, time.Since(start), beforeIp, newIp, session.IsClosed())
}(time.Now())
for {
retry++
status := command(cfg)
pingBaidu := PingBaidu()
getty.GetLogger().Warnf("[redial] net check: %d->%v", retry, pingBaidu)
if pingBaidu && status {
break
}
}
}
func command(cfg *model.Configure) bool {
defer func(start time.Time) {
getty.GetLogger().Warnf("[redial] cost %v", time.Since(start))
}(time.Now())
execPath := cfg.Redial.ExecPath
if len(execPath) == 0 {
getty.GetLogger().Warn("[redial] exec file is empty")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment