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 ...@@ -106,7 +106,7 @@ func (client *Client) handleUpStream(conn *net.TCPConn, packet *protocol.MajoraP
buf := make([]byte, common.BufSize) // 4k buf := make([]byte, common.BufSize) // 4k
cnt, err := conn.Read(buf) cnt, err := conn.Read(buf)
if err != nil { 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()) packet.SerialNumber, err, conn.LocalAddr(), conn.RemoteAddr())
client.OnClose(session, conn, packet.SerialNumber) client.OnClose(session, conn, packet.SerialNumber)
break break
...@@ -116,7 +116,7 @@ func (client *Client) handleUpStream(conn *net.TCPConn, packet *protocol.MajoraP ...@@ -116,7 +116,7 @@ func (client *Client) handleUpStream(conn *net.TCPConn, packet *protocol.MajoraP
pack.SerialNumber = packet.SerialNumber pack.SerialNumber = packet.SerialNumber
if _, _, err := session.WritePkg(pack, common.SessionTimeout); err != nil { 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) client.OnClose(session, conn, packet.SerialNumber)
break break
} else { } else {
......
...@@ -2,6 +2,7 @@ package client ...@@ -2,6 +2,7 @@ package client
import ( import (
"fmt" "fmt"
"math/rand"
"net" "net"
"time" "time"
...@@ -68,8 +69,12 @@ func InitialSession(session getty.Session, client *Client) (err error) { ...@@ -68,8 +69,12 @@ func InitialSession(session getty.Session, client *Client) (err error) {
session.SetWaitTime(common.WaitTimeout) session.SetWaitTime(common.WaitTimeout)
if client.config.Redial.Valid() { if client.config.Redial.Valid() {
getty.GetLogger().Infof("ReconnInterval %+v", client.config.Redial.RedialDuration) rand.Seed(time.Now().UnixNano())
session.SetCronPeriod(int(client.config.Redial.RedialDuration.Milliseconds())) // 加上随机 防止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.SetPkgHandler(PkgCodec)
session.SetEventListener(&MajoraEventListener{ session.SetEventListener(&MajoraEventListener{
......
...@@ -50,7 +50,7 @@ const ( ...@@ -50,7 +50,7 @@ const (
WaitTimeout = time.Minute WaitTimeout = time.Minute
KeepAliveTimeout = time.Second * 10 KeepAliveTimeout = time.Second * 10
SessionTimeout = time.Minute SessionTimeout = time.Minute * 5
UpstreamTimeout = time.Minute UpstreamTimeout = time.Minute
) )
...@@ -72,7 +72,7 @@ const ( ...@@ -72,7 +72,7 @@ const (
var ( var (
ErrInvalidSize = errors.New("invalid size") ErrInvalidSize = errors.New("invalid size")
ErrInvalidMagic = errors.New("invalid magic") ErrInvalidMagic = errors.New("invalid magic")
ErrNilPacket = errors.New("nil packet") ErrNilPacket = errors.New("nil packet")
) )
func ConvertInt32ToBytes(input int32) []byte { func ConvertInt32ToBytes(input int32) []byte {
......
tunnel_addr = 127.0.0.1:5879 ;tunnel_addr = 127.0.0.1:5879
;tunnel_addr = aoba.vip:5879 tunnel_addr = aoba.vip:5879
dns_server = 114.114.114.114:53 dns_server = 114.114.114.114:53
;bind to local ip ;bind to local ip
;local_ip = 192.168.0.100 ;local_ip = 192.168.0.100
......
...@@ -10,6 +10,7 @@ rm -fr start.sh ...@@ -10,6 +10,7 @@ rm -fr start.sh
rm -fr majora.ini rm -fr majora.ini
rm -fr majora.service rm -fr majora.service
rm -fr majora-dev.ini rm -fr majora-dev.ini
rm -fr majora.log
mv -f majora-cli*/* . mv -f majora-cli*/* .
...@@ -20,6 +21,10 @@ ln -s /root/majora.service /usr/lib/systemd/system/majora.service ...@@ -20,6 +21,10 @@ ln -s /root/majora.service /usr/lib/systemd/system/majora.service
systemctl daemon-reload systemctl daemon-reload
echo "start on reboot ..."
systemctl enable majora.service
echo "restart..." echo "restart..."
systemctl restart majora.service systemctl restart majora.service
......
...@@ -15,7 +15,7 @@ var ( ...@@ -15,7 +15,7 @@ var (
) )
const ( const (
defTimeout = time.Second * 10 defTimeout = time.Second * 5
baiduUrl = "https://www.baidu.com" baiduUrl = "https://www.baidu.com"
) )
...@@ -23,6 +23,7 @@ const ( ...@@ -23,6 +23,7 @@ const (
func init() { func init() {
httpcli = &http.Client{ httpcli = &http.Client{
Transport: &http.Transport{ Transport: &http.Transport{
TLSHandshakeTimeout: defTimeout,
TLSClientConfig: &tls.Config{ TLSClientConfig: &tls.Config{
InsecureSkipVerify: true, 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 ( ...@@ -15,24 +15,26 @@ const (
) )
func Redial(cfg *model.Configure, session getty.Session) { func Redial(cfg *model.Configure, session getty.Session) {
getty.GetLogger().Warnf("[redial] start, session status:%s", session.Stat()) getty.GetLogger().Warnf("[redial] start, session is close :%d", session.IsClosed())
status := command(cfg) beforeIp := GetPPP()
getty.GetLogger().Warnf("[redial] end:%v, session status: %s", status, session.Stat()) retry := 0
defer func(start time.Time) {
pingBaidu := PingBaidu() newIp := GetPPP()
getty.GetLogger().Warnf("[redial] net check: %v", pingBaidu) getty.GetLogger().Warnf("[redial] retry %d, cost %v, ip change %s -> %s, session is close:%v",
retry, time.Since(start), beforeIp, newIp, session.IsClosed())
if !pingBaidu { }(time.Now())
getty.GetLogger().Warnf("[redial] net check fail,redial...") for {
Redial(cfg, session) 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 { 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 execPath := cfg.Redial.ExecPath
if len(execPath) == 0 { if len(execPath) == 0 {
getty.GetLogger().Warn("[redial] exec file is empty") 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