Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
M
majora-go
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
majora
majora-go
Commits
76aca1d4
Commit
76aca1d4
authored
Nov 12, 2021
by
wei.xuan
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch '1107' into 'master'
feat:add ip See merge request
!8
parents
cfa24f9d
319274ed
Show whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
120 additions
and
23 deletions
+120
-23
event.go
client/event.go
+2
-2
getty.go
client/getty.go
+7
-2
common.go
common/common.go
+2
-2
majora-dev.ini
conf/majora-dev.ini
+2
-2
update.sh
conf/update.sh
+5
-0
httpping.go
infra/httpping.go
+2
-1
ip.go
infra/ip.go
+50
-0
ip_test.go
infra/ip_test.go
+34
-0
redail.go
infra/redail.go
+16
-14
No files found.
client/event.go
View file @
76aca1d4
...
...
@@ -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
()
.
Error
f
(
"[handleUpStream] %d->read with error:%+v,l:%s->r:%s"
,
getty
.
GetLogger
()
.
Warn
f
(
"[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
()
.
Debug
f
(
"[handleUpStream] %d-> write to server fail %+v"
,
packet
.
SerialNumber
,
err
.
Error
())
getty
.
GetLogger
()
.
Error
f
(
"[handleUpStream] %d-> write to server fail %+v"
,
packet
.
SerialNumber
,
err
.
Error
())
client
.
OnClose
(
session
,
conn
,
packet
.
SerialNumber
)
break
}
else
{
...
...
client/getty.go
View file @
76aca1d4
...
...
@@ -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
{
...
...
common/common.go
View file @
76aca1d4
...
...
@@ -50,7 +50,7 @@ const (
WaitTimeout
=
time
.
Minute
KeepAliveTimeout
=
time
.
Second
*
10
SessionTimeout
=
time
.
Minute
SessionTimeout
=
time
.
Minute
*
5
UpstreamTimeout
=
time
.
Minute
)
...
...
conf/majora-dev.ini
View file @
76aca1d4
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
...
...
conf/update.sh
View file @
76aca1d4
...
...
@@ -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
...
...
infra/httpping.go
View file @
76aca1d4
...
...
@@ -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
,
},
...
...
infra/ip.go
0 → 100644
View file @
76aca1d4
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
()
}
infra/ip_test.go
0 → 100644
View file @
76aca1d4
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
)
}
})
}
}
infra/redail.go
View file @
76aca1d4
...
...
@@ -15,24 +15,26 @@ const (
)
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
())
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
)
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] 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"
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment