Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
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
ecf22296
Commit
ecf22296
authored
Nov 06, 2021
by
wei.xuan
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'fix_codec' into 'master'
fix:codec See merge request
!6
parents
680eed47
9587967d
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
43 additions
and
40 deletions
+43
-40
Makefile
Makefile
+1
-1
cli.go
cli.go
+10
-7
codec.go
client/codec.go
+20
-10
event.go
client/event.go
+2
-3
getty.go
client/getty.go
+2
-2
common.go
common/common.go
+3
-12
majora-dev.ini
conf/majora-dev.ini
+2
-2
majora.ini
conf/majora.ini
+2
-2
redail.go
infra/redail.go
+1
-1
No files found.
Makefile
View file @
ecf22296
...
@@ -3,7 +3,7 @@ export GOPROXY="https://goproxy.cn,https://goproxy.io,direct"
...
@@ -3,7 +3,7 @@ export GOPROXY="https://goproxy.cn,https://goproxy.io,direct"
LDFLAGS
:=
-s
-w
LDFLAGS
:=
-s
-w
DATE
=
$(
shell
date
+
"%Y-%m-%d"
)
DATE
=
$(
shell
date
+
"%Y-%m-%d"
)
BUILDINFO
:=
-X
main.Version
=
v0.0.5
-X
main.Date
=
$(DATE)
BUILDINFO
:=
-X
main.Version
=
latest
-X
main.Date
=
$(DATE)
all
:
all
:
env
CGO_ENABLED
=
0 go build
-trimpath
-ldflags
'-w -s
$(BUILDINFO)
'
-o
bin/majora
env
CGO_ENABLED
=
0 go build
-trimpath
-ldflags
'-w -s
$(BUILDINFO)
'
-o
bin/majora
...
...
cli.go
View file @
ecf22296
...
@@ -13,13 +13,13 @@ import (
...
@@ -13,13 +13,13 @@ import (
"os"
"os"
"os/signal"
"os/signal"
"runtime"
"runtime"
"runtime/debug"
"syscall"
"syscall"
"time"
"time"
"github.com/adamweixuan/getty"
"github.com/adamweixuan/getty"
"virjar.com/majora-go/client"
"virjar.com/majora-go/client"
"virjar.com/majora-go/common"
"virjar.com/majora-go/common"
"virjar.com/majora-go/infra"
"virjar.com/majora-go/model"
"virjar.com/majora-go/model"
)
)
...
@@ -59,9 +59,9 @@ func initial(cfg *model.Configure) {
...
@@ -59,9 +59,9 @@ func initial(cfg *model.Configure) {
_
=
getty
.
SetLoggerLevel
(
getty
.
LoggerLevel
(
cfg
.
LogLevel
-
1
))
_
=
getty
.
SetLoggerLevel
(
getty
.
LoggerLevel
(
cfg
.
LogLevel
-
1
))
}
}
addr
:=
fmt
.
Sprintf
(
"127.0.0.1:%d"
,
cfg
.
PprofPort
)
addr
:=
fmt
.
Sprintf
(
"127.0.0.1:%d"
,
cfg
.
PprofPort
)
getty
.
GetLogger
()
.
Infof
(
"enable pprof: %s"
,
addr
)
if
cfg
.
PprofPort
>
0
{
if
cfg
.
PprofPort
>
0
{
go
func
()
{
go
func
()
{
getty
.
GetLogger
()
.
Infof
(
"enable pprof: %s"
,
addr
)
log
.
Fatal
(
http
.
ListenAndServe
(
addr
,
nil
))
log
.
Fatal
(
http
.
ListenAndServe
(
addr
,
nil
))
}()
}()
}
}
...
@@ -76,11 +76,11 @@ func initial(cfg *model.Configure) {
...
@@ -76,11 +76,11 @@ func initial(cfg *model.Configure) {
getty
.
GetLogger
()
.
Infof
(
"use custom dns server %s"
,
dnsServer
)
getty
.
GetLogger
()
.
Infof
(
"use custom dns server %s"
,
dnsServer
)
}
}
if
!
cfg
.
DisableUpdate
{
//
if !cfg.DisableUpdate {
if
_
,
err
:=
infra
.
Update
(
"majora"
,
Version
);
err
!=
nil
{
//
if _, err := infra.Update("majora", Version); err != nil {
getty
.
GetLogger
()
.
Errorf
(
"check update error %+s"
,
err
.
Error
())
//
getty.GetLogger().Errorf("check update error %+s", err.Error())
}
//
}
}
//
}
}
}
func
parseFromCmd
(
cfg
*
model
.
Configure
)
{
func
parseFromCmd
(
cfg
*
model
.
Configure
)
{
...
@@ -116,6 +116,9 @@ func main() {
...
@@ -116,6 +116,9 @@ func main() {
fmt
.
Println
(
Version
)
fmt
.
Println
(
Version
)
os
.
Exit
(
0
)
os
.
Exit
(
0
)
}
}
runtime
.
GOMAXPROCS
(
runtime
.
NumCPU
()
*
2
)
debug
.
SetGCPercent
(
200
)
getty
.
GetLogger
()
.
Warnf
(
"cpu count %d,proc %d"
,
runtime
.
NumCPU
(),
runtime
.
NumCPU
()
*
2
)
sigs
:=
make
(
chan
os
.
Signal
,
1
)
sigs
:=
make
(
chan
os
.
Signal
,
1
)
signal
.
Notify
(
sigs
,
syscall
.
SIGINT
,
syscall
.
SIGTERM
)
signal
.
Notify
(
sigs
,
syscall
.
SIGINT
,
syscall
.
SIGTERM
)
...
...
client/codec.go
View file @
ecf22296
...
@@ -3,7 +3,6 @@ package client
...
@@ -3,7 +3,6 @@ package client
import
(
import
(
"bytes"
"bytes"
"encoding/binary"
"encoding/binary"
"errors"
"github.com/adamweixuan/getty"
"github.com/adamweixuan/getty"
"virjar.com/majora-go/common"
"virjar.com/majora-go/common"
...
@@ -24,7 +23,11 @@ func (p *PacketCodec) Read(session getty.Session, data []byte) (interface{}, int
...
@@ -24,7 +23,11 @@ func (p *PacketCodec) Read(session getty.Session, data []byte) (interface{}, int
}
}
getty
.
GetLogger
()
.
Debugf
(
"[PacketCodec] read magic %+v"
,
binary
.
BigEndian
.
Uint64
(
data
[
0
:
8
]))
getty
.
GetLogger
()
.
Debugf
(
"[PacketCodec] read magic %+v"
,
binary
.
BigEndian
.
Uint64
(
data
[
0
:
8
]))
if
!
common
.
ReadMagic
(
data
[
0
:
common
.
MagicSize
])
{
readmagic
:=
data
[
0
:
common
.
MagicSize
]
if
!
common
.
ReadMagic
(
readmagic
)
{
getty
.
GetLogger
()
.
Errorf
(
"[PacketCodec] invalid magic %d|%s"
,
binary
.
BigEndian
.
Uint64
(
readmagic
),
string
(
readmagic
))
return
nil
,
0
,
common
.
ErrInvalidMagic
return
nil
,
0
,
common
.
ErrInvalidMagic
}
}
...
@@ -33,16 +36,22 @@ func (p *PacketCodec) Read(session getty.Session, data []byte) (interface{}, int
...
@@ -33,16 +36,22 @@ func (p *PacketCodec) Read(session getty.Session, data []byte) (interface{}, int
frameLen
,
err
:=
common
.
ReadInt32
(
reader
)
frameLen
,
err
:=
common
.
ReadInt32
(
reader
)
if
err
!=
nil
{
if
err
!=
nil
{
getty
.
GetLogger
()
.
Errorf
(
"[PacketCodec] frameLen error %+v"
,
err
)
getty
.
GetLogger
()
.
Errorf
(
"[PacketCodec] frameLen error %+v"
,
err
)
return
nil
,
0
,
common
.
ErrInvalidSize
return
nil
,
0
,
err
}
}
if
reader
.
Len
()
<
int
(
frameLen
)
{
getty
.
GetLogger
()
.
Errorf
(
"[PacketCodec] buf not enough %d|%d"
,
reader
.
Len
(),
frameLen
)
return
nil
,
0
,
nil
}
getty
.
GetLogger
()
.
Debugf
(
"[PacketCodec] read frameLen %d"
,
frameLen
)
getty
.
GetLogger
()
.
Debugf
(
"[PacketCodec] read frameLen %d"
,
frameLen
)
// type
// type
msgType
,
err
:=
common
.
ReadByte
(
reader
)
msgType
,
err
:=
common
.
ReadByte
(
reader
)
if
err
!=
nil
{
if
err
!=
nil
{
getty
.
GetLogger
()
.
Errorf
(
"[PacketCodec] read type error %+v"
,
err
)
getty
.
GetLogger
()
.
Errorf
(
"[PacketCodec] read type error %+v"
,
err
)
return
nil
,
0
,
common
.
ErrInvalidSize
return
nil
,
0
,
err
}
}
getty
.
GetLogger
()
.
Debugf
(
"[PacketCodec] read msgType %+v"
,
msgType
)
getty
.
GetLogger
()
.
Debugf
(
"[PacketCodec] read msgType %+v"
,
msgType
)
...
@@ -54,7 +63,7 @@ func (p *PacketCodec) Read(session getty.Session, data []byte) (interface{}, int
...
@@ -54,7 +63,7 @@ func (p *PacketCodec) Read(session getty.Session, data []byte) (interface{}, int
pack
.
SerialNumber
,
err
=
common
.
ReadInt64
(
reader
)
pack
.
SerialNumber
,
err
=
common
.
ReadInt64
(
reader
)
if
err
!=
nil
{
if
err
!=
nil
{
getty
.
GetLogger
()
.
Errorf
(
"[PacketCodec] read num error %+v"
,
err
)
getty
.
GetLogger
()
.
Errorf
(
"[PacketCodec] read num error %+v"
,
err
)
return
nil
,
0
,
common
.
ErrInvalidSize
return
nil
,
len
(
data
),
nil
}
}
getty
.
GetLogger
()
.
Debugf
(
"[PacketCodec] read SerialNumber %d"
,
pack
.
SerialNumber
)
getty
.
GetLogger
()
.
Debugf
(
"[PacketCodec] read SerialNumber %d"
,
pack
.
SerialNumber
)
...
@@ -63,13 +72,13 @@ func (p *PacketCodec) Read(session getty.Session, data []byte) (interface{}, int
...
@@ -63,13 +72,13 @@ func (p *PacketCodec) Read(session getty.Session, data []byte) (interface{}, int
extraSize
,
err
:=
common
.
ReadByte
(
reader
)
extraSize
,
err
:=
common
.
ReadByte
(
reader
)
if
err
!=
nil
{
if
err
!=
nil
{
getty
.
GetLogger
()
.
Errorf
(
"[PacketCodec] read extra size error %+v"
,
err
)
getty
.
GetLogger
()
.
Errorf
(
"[PacketCodec] read extra size error %+v"
,
err
)
return
nil
,
0
,
common
.
ErrInvalidSize
return
nil
,
len
(
data
),
nil
}
}
extra
,
err
:=
common
.
ReadN
(
int
(
extraSize
),
reader
)
extra
,
err
:=
common
.
ReadN
(
int
(
extraSize
),
reader
)
if
err
!=
nil
{
if
err
!=
nil
{
getty
.
GetLogger
()
.
Errorf
(
"[PacketCodec] read extra error %+v"
,
err
)
getty
.
GetLogger
()
.
Errorf
(
"[PacketCodec] read extra error %+v"
,
err
)
return
nil
,
0
,
common
.
ErrInvalidSize
return
nil
,
len
(
data
),
nil
}
}
pack
.
Extra
=
string
(
extra
)
pack
.
Extra
=
string
(
extra
)
getty
.
GetLogger
()
.
Debugf
(
"[PacketCodec] read extra %s"
,
pack
.
Extra
)
getty
.
GetLogger
()
.
Debugf
(
"[PacketCodec] read extra %s"
,
pack
.
Extra
)
...
@@ -78,13 +87,14 @@ func (p *PacketCodec) Read(session getty.Session, data []byte) (interface{}, int
...
@@ -78,13 +87,14 @@ func (p *PacketCodec) Read(session getty.Session, data []byte) (interface{}, int
dataSize
:=
int
(
frameLen
)
-
common
.
TypeSize
-
common
.
SerialNumberSize
-
common
.
ExtraSize
-
int
(
extraSize
)
dataSize
:=
int
(
frameLen
)
-
common
.
TypeSize
-
common
.
SerialNumberSize
-
common
.
ExtraSize
-
int
(
extraSize
)
if
dataSize
<
0
{
if
dataSize
<
0
{
getty
.
GetLogger
()
.
Errorf
(
"[PacketCodec] read frameLen error %+v"
,
err
)
getty
.
GetLogger
()
.
Errorf
(
"[PacketCodec] read frameLen error %+v"
,
err
)
return
nil
,
0
,
common
.
ErrInvalidSize
return
nil
,
len
(
data
)
,
common
.
ErrInvalidSize
}
}
if
dataSize
>
0
{
if
dataSize
>
0
{
data
,
err
:=
common
.
ReadN
(
dataSize
,
reader
)
data
,
err
:=
common
.
ReadN
(
dataSize
,
reader
)
if
err
!=
nil
{
if
err
!=
nil
{
getty
.
GetLogger
()
.
Errorf
(
"read data error %+v"
,
err
)
getty
.
GetLogger
()
.
Errorf
(
"[PacketCodec] read data error %+v"
,
err
)
return
nil
,
len
(
data
),
nil
}
}
pack
.
Data
=
data
pack
.
Data
=
data
}
}
...
@@ -95,7 +105,7 @@ func (p *PacketCodec) Read(session getty.Session, data []byte) (interface{}, int
...
@@ -95,7 +105,7 @@ func (p *PacketCodec) Read(session getty.Session, data []byte) (interface{}, int
func
(
p
*
PacketCodec
)
Write
(
session
getty
.
Session
,
packet
interface
{})
([]
byte
,
error
)
{
func
(
p
*
PacketCodec
)
Write
(
session
getty
.
Session
,
packet
interface
{})
([]
byte
,
error
)
{
majoraPkt
,
ok
:=
packet
.
(
*
protocol
.
MajoraPacket
)
majoraPkt
,
ok
:=
packet
.
(
*
protocol
.
MajoraPacket
)
if
!
ok
{
if
!
ok
{
return
nil
,
errors
.
New
(
"invalid packet"
)
return
nil
,
common
.
ErrNilPacket
}
}
return
protocol
.
Codec
.
Encode
(
majoraPkt
),
nil
return
protocol
.
Codec
.
Encode
(
majoraPkt
),
nil
}
}
client/event.go
View file @
ecf22296
...
@@ -148,9 +148,8 @@ func (client *Client) handleDestroyMessage() {
...
@@ -148,9 +148,8 @@ func (client *Client) handleDestroyMessage() {
}
}
func
(
client
*
Client
)
AddConnection
(
packet
*
protocol
.
MajoraPacket
,
conn
*
net
.
TCPConn
,
addr
string
)
{
func
(
client
*
Client
)
AddConnection
(
packet
*
protocol
.
MajoraPacket
,
conn
*
net
.
TCPConn
,
addr
string
)
{
load
,
ok
:=
client
.
connStore
.
Load
(
packet
.
SerialNumber
)
if
_
,
ok
:=
client
.
connStore
.
Load
(
packet
.
SerialNumber
);
ok
{
if
ok
{
getty
.
GetLogger
()
.
Errorf
(
"[AddConnection] %d->error, has one"
,
packet
.
SerialNumber
)
getty
.
GetLogger
()
.
Errorf
(
"[AddConnection] %d->error, has one %+v..."
,
packet
.
SerialNumber
,
load
)
}
}
client
.
connStore
.
Store
(
packet
.
SerialNumber
,
conn
)
client
.
connStore
.
Store
(
packet
.
SerialNumber
,
conn
)
getty
.
GetLogger
()
.
Infof
(
"[AddConnection] %d->%s success"
,
packet
.
SerialNumber
,
addr
)
getty
.
GetLogger
()
.
Infof
(
"[AddConnection] %d->%s success"
,
packet
.
SerialNumber
,
addr
)
...
...
client/getty.go
View file @
ecf22296
...
@@ -12,7 +12,7 @@ import (
...
@@ -12,7 +12,7 @@ import (
)
)
var
(
var
(
taskPool
=
gxsync
.
NewTaskPoolSimple
(
10
000
)
taskPool
=
gxsync
.
NewTaskPoolSimple
(
5
000
)
)
)
func
(
client
*
Client
)
connect
()
{
func
(
client
*
Client
)
connect
()
{
...
@@ -79,7 +79,7 @@ func InitialSession(session getty.Session, client *Client) (err error) {
...
@@ -79,7 +79,7 @@ func InitialSession(session getty.Session, client *Client) (err error) {
}
}
func
(
client
*
Client
)
Redial
(
session
getty
.
Session
)
{
func
(
client
*
Client
)
Redial
(
session
getty
.
Session
)
{
getty
.
GetLogger
()
.
Warnf
(
"=================redial start =============="
,
client
.
config
.
Redial
.
Valid
())
getty
.
GetLogger
()
.
Warnf
(
"=================redial start
%v
=============="
,
client
.
config
.
Redial
.
Valid
())
if
!
client
.
config
.
Redial
.
Valid
()
{
if
!
client
.
config
.
Redial
.
Valid
()
{
return
return
}
}
...
...
common/common.go
View file @
ecf22296
...
@@ -6,8 +6,6 @@ import (
...
@@ -6,8 +6,6 @@ import (
"errors"
"errors"
"io"
"io"
"time"
"time"
"github.com/adamweixuan/getty"
)
)
const
(
const
(
...
@@ -49,10 +47,10 @@ const (
...
@@ -49,10 +47,10 @@ const (
const
(
const
(
ReadTimeout
=
time
.
Minute
ReadTimeout
=
time
.
Minute
WriteTimeout
=
time
.
Minute
WriteTimeout
=
time
.
Minute
WaitTimeout
=
time
.
Second
*
30
WaitTimeout
=
time
.
Minute
KeepAliveTimeout
=
time
.
Second
*
10
KeepAliveTimeout
=
time
.
Second
*
10
SessionTimeout
=
time
.
Second
*
30
SessionTimeout
=
time
.
Minute
UpstreamTimeout
=
time
.
Minute
UpstreamTimeout
=
time
.
Minute
)
)
...
@@ -68,17 +66,13 @@ const (
...
@@ -68,17 +66,13 @@ const (
)
)
const
(
const
(
ConnTimeout
=
time
.
Second
*
30
ReConnInterval
=
time
.
Second
*
5
)
const
(
ExtrakeyUser
=
"majora.key.user"
ExtrakeyUser
=
"majora.key.user"
)
)
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"
)
)
)
func
ConvertInt32ToBytes
(
input
int32
)
[]
byte
{
func
ConvertInt32ToBytes
(
input
int32
)
[]
byte
{
...
@@ -137,8 +131,5 @@ func ReadN(size int, conn io.Reader) ([]byte, error) {
...
@@ -137,8 +131,5 @@ func ReadN(size int, conn io.Reader) ([]byte, error) {
func
ReadMagic
(
buf
[]
byte
)
bool
{
func
ReadMagic
(
buf
[]
byte
)
bool
{
magic
:=
int64
(
binary
.
BigEndian
.
Uint64
(
buf
))
magic
:=
int64
(
binary
.
BigEndian
.
Uint64
(
buf
))
if
magic
!=
MAGIC
{
getty
.
GetLogger
()
.
Errorf
(
"magic not equal %d|%d|%+v"
,
magic
,
MAGIC
,
buf
)
}
return
magic
==
MAGIC
return
magic
==
MAGIC
}
}
conf/majora-dev.ini
View file @
ecf22296
;
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/majora.ini
View file @
ecf22296
...
@@ -6,7 +6,7 @@ dns_server = 114.114.114.114:53
...
@@ -6,7 +6,7 @@ dns_server = 114.114.114.114:53
;pprof_port = 0
;pprof_port = 0
disable_update
=
false
disable_update
=
false
; default is info
; default is info
log_level
=
1
log_level
=
2
reconn_interval
=
5s
reconn_interval
=
5s
;client_id =
;client_id =
...
@@ -16,5 +16,5 @@ account = superman
...
@@ -16,5 +16,5 @@ account = superman
[redial]
[redial]
command
=
/bin/bash
command
=
/bin/bash
exec_path
=
/root/ppp_auto_with_auth.sh
exec_path
=
/root/ppp_auto_with_auth.sh
redial_duration
=
6
m
redial_duration
=
10
m
wait_time
=
10s
wait_time
=
10s
infra/redail.go
View file @
ecf22296
...
@@ -36,5 +36,5 @@ func Redial(cfg *model.Configure) {
...
@@ -36,5 +36,5 @@ func Redial(cfg *model.Configure) {
getty
.
GetLogger
()
.
Errorf
(
"[redial] Execute Shell:%s failed with error:%s"
,
command
,
err
.
Error
())
getty
.
GetLogger
()
.
Errorf
(
"[redial] Execute Shell:%s failed with error:%s"
,
command
,
err
.
Error
())
return
return
}
}
getty
.
GetLogger
()
.
Info
f
(
"[redial] redial success %+v resp:%s"
,
cmd
,
string
(
output
))
getty
.
GetLogger
()
.
Warn
f
(
"[redial] redial success %+v resp:%s"
,
cmd
,
string
(
output
))
}
}
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