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
Hide 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
...
@@ -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
()
.
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
())
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
()
.
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
)
client
.
OnClose
(
session
,
conn
,
packet
.
SerialNumber
)
break
break
}
else
{
}
else
{
...
...
client/getty.go
View file @
76aca1d4
...
@@ -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
{
...
...
common/common.go
View file @
76aca1d4
...
@@ -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
{
...
...
conf/majora-dev.ini
View file @
76aca1d4
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
...
...
conf/update.sh
View file @
76aca1d4
...
@@ -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
...
...
infra/httpping.go
View file @
76aca1d4
...
@@ -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
,
},
},
...
...
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 (
...
@@ -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"
)
...
...
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