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
5266865f
Commit
5266865f
authored
Jan 09, 2022
by
Tsaiilin(蔡依林)
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'transfer' into 'master'
Transfer See merge request
!13
parents
f5abc227
ec9ec7e2
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
246 additions
and
179 deletions
+246
-179
cli.go
cli.go
+1
-1
client.go
client/client.go
+13
-16
cluster.go
client/cluster.go
+3
-3
event.go
client/event.go
+117
-141
getty.go
client/getty.go
+1
-1
transfer.go
client/transfer.go
+100
-0
majora-dev.yaml
conf/majora-dev.yaml
+7
-7
safe.go
safe/safe.go
+1
-1
trace.go
trace/trace.go
+3
-9
No files found.
cli.go
View file @
5266865f
...
@@ -46,7 +46,7 @@ func init() {
...
@@ -46,7 +46,7 @@ func init() {
func
initial
()
{
func
initial
()
{
if
global
.
Config
.
PprofPort
>
0
{
if
global
.
Config
.
PprofPort
>
0
{
safe
.
Safe
Go
(
func
()
{
safe
.
Go
(
func
()
{
addr
:=
fmt
.
Sprintf
(
"127.0.0.1:%d"
,
global
.
Config
.
PprofPort
)
addr
:=
fmt
.
Sprintf
(
"127.0.0.1:%d"
,
global
.
Config
.
PprofPort
)
log
.
Run
()
.
Infof
(
"enable pprof: %s"
,
addr
)
log
.
Run
()
.
Infof
(
"enable pprof: %s"
,
addr
)
log
.
Run
()
.
Error
(
http
.
ListenAndServe
(
addr
,
nil
))
log
.
Run
()
.
Error
(
http
.
ListenAndServe
(
addr
,
nil
))
...
...
client/client.go
View file @
5266865f
...
@@ -17,8 +17,7 @@ type Client struct {
...
@@ -17,8 +17,7 @@ type Client struct {
localAddr
net
.
Addr
localAddr
net
.
Addr
natTunnel
getty
.
Client
natTunnel
getty
.
Client
session
getty
.
Session
session
getty
.
Session
connStore
sync
.
Map
transferStore
sync
.
Map
sessionStore
sync
.
Map
dnsCache
*
freecache
.
Cache
dnsCache
*
freecache
.
Cache
}
}
...
@@ -39,8 +38,7 @@ func NewCli(cfg *model.Configure, host string, port int) *Client {
...
@@ -39,8 +38,7 @@ func NewCli(cfg *model.Configure, host string, port int) *Client {
host
:
host
,
host
:
host
,
port
:
port
,
port
:
port
,
localAddr
:
localAddr
,
localAddr
:
localAddr
,
connStore
:
sync
.
Map
{},
transferStore
:
sync
.
Map
{},
sessionStore
:
sync
.
Map
{},
dnsCache
:
freecache
.
NewCache
(
1024
),
dnsCache
:
freecache
.
NewCache
(
1024
),
}
}
...
@@ -50,4 +48,3 @@ func NewCli(cfg *model.Configure, host string, port int) *Client {
...
@@ -50,4 +48,3 @@ func NewCli(cfg *model.Configure, host string, port int) *Client {
func
(
client
*
Client
)
StartUp
()
{
func
(
client
*
Client
)
StartUp
()
{
client
.
connect
()
client
.
connect
()
}
}
client/cluster.go
View file @
5266865f
...
@@ -23,7 +23,7 @@ type ClusterClient struct {
...
@@ -23,7 +23,7 @@ type ClusterClient struct {
func
(
c
*
ClusterClient
)
Start
()
{
func
(
c
*
ClusterClient
)
Start
()
{
c
.
check
()
c
.
check
()
safe
.
Safe
Go
(
func
()
{
safe
.
Go
(
func
()
{
var
timer
=
time
.
NewTimer
(
5
*
time
.
Minute
)
var
timer
=
time
.
NewTimer
(
5
*
time
.
Minute
)
for
{
for
{
c
.
connectNatServers
()
c
.
connectNatServers
()
...
@@ -32,7 +32,7 @@ func (c *ClusterClient) Start() {
...
@@ -32,7 +32,7 @@ func (c *ClusterClient) Start() {
}
}
})
})
if
global
.
Config
.
Redial
.
Valid
()
{
if
global
.
Config
.
Redial
.
Valid
()
{
safe
.
Safe
Go
(
func
()
{
safe
.
Go
(
func
()
{
// 加上随机 防止vps在同时间重启
// 加上随机 防止vps在同时间重启
duration
:=
c
.
randomDuration
()
duration
:=
c
.
randomDuration
()
log
.
Run
()
.
Infof
(
"Redial interval %+v"
,
duration
)
log
.
Run
()
.
Infof
(
"Redial interval %+v"
,
duration
)
...
@@ -142,7 +142,7 @@ func (c *ClusterClient) check() {
...
@@ -142,7 +142,7 @@ func (c *ClusterClient) check() {
url
=
global
.
Config
.
NetCheckUrl
url
=
global
.
Config
.
NetCheckUrl
}
}
safe
.
Safe
Go
(
func
()
{
safe
.
Go
(
func
()
{
var
timer
=
time
.
NewTimer
(
interval
)
var
timer
=
time
.
NewTimer
(
interval
)
for
{
for
{
timer
.
Reset
(
interval
)
timer
.
Reset
(
interval
)
...
...
client/event.go
View file @
5266865f
This diff is collapsed.
Click to expand it.
client/getty.go
View file @
5266865f
...
@@ -75,7 +75,7 @@ func (client *Client) Redial(tag string) {
...
@@ -75,7 +75,7 @@ func (client *Client) Redial(tag string) {
if
!
client
.
config
.
Redial
.
Valid
()
{
if
!
client
.
config
.
Redial
.
Valid
()
{
return
return
}
}
log
.
Run
()
.
Infof
(
"[Redial %s]
Sen
d offline message"
,
tag
)
log
.
Run
()
.
Infof
(
"[Redial %s]
see
d offline message"
,
tag
)
if
_
,
_
,
err
:=
client
.
session
.
WritePkg
(
OfflinePacket
,
0
);
err
!=
nil
{
if
_
,
_
,
err
:=
client
.
session
.
WritePkg
(
OfflinePacket
,
0
);
err
!=
nil
{
log
.
Run
()
.
Errorf
(
"[Redial %s] write offline to server error %s"
,
tag
,
err
.
Error
())
log
.
Run
()
.
Errorf
(
"[Redial %s] write offline to server error %s"
,
tag
,
err
.
Error
())
}
}
...
...
client/transfer.go
0 → 100644
View file @
5266865f
package
client
import
(
"errors"
"fmt"
"net"
"sync"
"time"
"virjar.com/majora-go/common"
"virjar.com/majora-go/log"
"virjar.com/majora-go/protocol"
"virjar.com/majora-go/safe"
"virjar.com/majora-go/trace"
)
type
Transfer
struct
{
serialNumber
int64
client
*
Client
upstreamConn
*
net
.
TCPConn
recorder
trace
.
Recorder
transferChan
chan
*
protocol
.
MajoraPacket
transferToUpstreamFunc
func
(
t
*
Transfer
,
p
*
protocol
.
MajoraPacket
)
transferToDownstreamFunc
func
(
t
*
Transfer
,
data
[]
byte
,
err
error
)
once
sync
.
Once
cancel
chan
struct
{}
}
func
NewTransfer
(
serialNumber
int64
,
client
*
Client
,
conn
*
net
.
TCPConn
,
recorder
trace
.
Recorder
)
*
Transfer
{
return
&
Transfer
{
serialNumber
:
serialNumber
,
client
:
client
,
upstreamConn
:
conn
,
recorder
:
recorder
,
transferChan
:
make
(
chan
*
protocol
.
MajoraPacket
,
10
),
once
:
sync
.
Once
{},
cancel
:
make
(
chan
struct
{},
0
),
}
}
func
(
t
*
Transfer
)
SetTransferToUpstreamFunc
(
f
func
(
t
*
Transfer
,
p
*
protocol
.
MajoraPacket
))
{
t
.
transferToUpstreamFunc
=
f
}
func
(
t
*
Transfer
)
SetTransferToDownstreamFunc
(
f
func
(
t
*
Transfer
,
data
[]
byte
,
err
error
))
{
t
.
transferToDownstreamFunc
=
f
}
func
(
t
*
Transfer
)
TransferToUpstream
(
p
*
protocol
.
MajoraPacket
)
{
t
.
transferChan
<-
p
}
func
(
t
*
Transfer
)
Start
()
{
if
t
.
transferToUpstreamFunc
==
nil
{
panic
(
errors
.
New
(
"transferToUpstreamFunc is nil"
))
}
if
t
.
transferToDownstreamFunc
==
nil
{
panic
(
errors
.
New
(
"transferToDownstreamFunc is nil"
))
}
safe
.
Go
(
func
()
{
for
{
select
{
case
p
:=
<-
t
.
transferChan
:
t
.
transferToUpstreamFunc
(
t
,
p
)
case
<-
t
.
cancel
:
return
}
}
})
safe
.
Go
(
func
()
{
traceRecorder
:=
t
.
recorder
traceRecorder
.
RecordEvent
(
trace
.
UpStreamEvent
,
fmt
.
Sprintf
(
"Ready read from upstream (sn:%d)"
,
t
.
serialNumber
))
log
.
Run
()
.
Debugf
(
"[handleUpStream] %d-> handleUpStream start..."
,
t
.
serialNumber
)
for
{
buf
:=
make
([]
byte
,
common
.
BufSize
)
cnt
,
err
:=
t
.
upstreamConn
.
Read
(
buf
)
t
.
transferToDownstreamFunc
(
t
,
buf
[
0
:
cnt
],
err
)
if
err
!=
nil
{
break
}
}
})
}
func
(
t
*
Transfer
)
Close
()
{
t
.
once
.
Do
(
func
()
{
readDeadLine
:=
time
.
Now
()
.
Add
(
3
*
time
.
Millisecond
)
t
.
recorder
.
RecordEvent
(
trace
.
DisconnectEvent
,
fmt
.
Sprintf
(
"Set upstream read deadline:%s (sn:%d)"
,
readDeadLine
.
Format
(
"2006-01-02 15:04:05.000000"
),
t
.
serialNumber
))
err
:=
t
.
upstreamConn
.
SetReadDeadline
(
readDeadLine
)
if
err
!=
nil
{
t
.
recorder
.
RecordErrorEvent
(
trace
.
DisconnectEvent
,
fmt
.
Sprintf
(
"Set upstream read deadline failed (sn:%d)"
,
t
.
serialNumber
),
err
)
_
=
t
.
upstreamConn
.
Close
()
}
close
(
t
.
cancel
)
})
}
conf/majora-dev.yaml
View file @
5266865f
tunnel_addr
:
majora-vps-zj.virjar.com
:5879
tunnel_addr
:
127.0.0.1
:5879
dns_server
:
114.114.114.114:53
dns_server
:
114.114.114.114:53
daemon
:
true
#
daemon: true
log_level
:
debug
log_level
:
debug
log_path
:
./majora-log/
log_path
:
./majora-log/
reconn_intervalz
:
5s
reconn_intervalz
:
5s
...
@@ -8,11 +8,11 @@ net_check_interval: 5s
...
@@ -8,11 +8,11 @@ net_check_interval: 5s
dns_cache_duration
:
10m
dns_cache_duration
:
10m
net_check_url
:
https://www.baidu.com[extra]
net_check_url
:
https://www.baidu.com[extra]
redial
:
#
redial:
command
:
/bin/bash
#
command: /bin/bash
exec_path
:
/root/ppp_redial.sh
#
exec_path: /root/ppp_redial.sh
redial_duration
:
5m
#
redial_duration: 5m
wait_time
:
10s
#
wait_time: 10s
extra
:
extra
:
account
:
superman
account
:
superman
safe/safe.go
View file @
5266865f
...
@@ -2,7 +2,7 @@ package safe
...
@@ -2,7 +2,7 @@ package safe
import
"virjar.com/majora-go/log"
import
"virjar.com/majora-go/log"
func
Safe
Go
(
f
func
())
{
func
Go
(
f
func
())
{
go
func
()
{
go
func
()
{
defer
func
()
{
defer
func
()
{
if
err
:=
recover
();
err
!=
nil
{
if
err
:=
recover
();
err
!=
nil
{
...
...
trace/trace.go
View file @
5266865f
...
@@ -22,7 +22,7 @@ var (
...
@@ -22,7 +22,7 @@ var (
)
)
func
init
()
{
func
init
()
{
safe
.
Safe
Go
(
func
()
{
safe
.
Go
(
func
()
{
for
{
for
{
e
:=
<-
sessionEventChan
e
:=
<-
sessionEventChan
if
e
.
Err
!=
nil
{
if
e
.
Err
!=
nil
{
...
@@ -133,15 +133,9 @@ func acquireRecorder(sessionId string, host, user string, enable bool) Recorder
...
@@ -133,15 +133,9 @@ func acquireRecorder(sessionId string, host, user string, enable bool) Recorder
}
}
type
Session
struct
{
func
NewRecorder
(
sessionId
string
,
host
string
,
user
string
,
enable
bool
)
Recorder
{
Recorder
Recorder
}
func
NewSession
(
sessionId
string
,
host
string
,
user
string
,
enable
bool
)
*
Session
{
if
len
(
sessionId
)
==
0
{
if
len
(
sessionId
)
==
0
{
sessionId
=
sessionIdNop
sessionId
=
sessionIdNop
}
}
return
&
Session
{
return
acquireRecorder
(
sessionId
,
host
,
user
,
enable
)
Recorder
:
acquireRecorder
(
sessionId
,
host
,
user
,
enable
),
}
}
}
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