Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge from pgshow #5

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions Change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

# CHANGE
go customDev.FiberServer()
go customDev.NpsTcpServer()

go customDev.Npc2Nps()
go customDev.Npc2Client()

customDev.VkeyWrong()

customDev.VersionWrong()

customDev.RemoteNpsIP = customDev.GetIpByStr(cnf.CommonConfig.Server)

LastConnectTime time.Time // 上次客户端成功建立连接的时间

v.LastConnectTime = time.Now() // 记录客户端存活时间
---
if (row.LastConnectTime === 0) {
return '<span class="badge badge-primary">从未连接</span>'
}
// 在列表页显示存活或者长期离线提示
let now = Date.parse(new Date())/1000;
let offMinute = parseInt((now - row.LastConnectTime)/60)
let msg

if (offMinute < 10) {
msg = offMinute +'分钟'
} else {
msg = '<span style=color:red>' + offMinute +'分钟</span>'
}

return '<span class="badge badge-badge" langtag="word-offline"></span> ' + msg
# TEST CODE
content := []byte("测试1\n测试2\n")
_ = ioutil.WriteFile("/home/pgshow/Desktop/nps/cmd/npc/111.txt", content, 0644)

# CAUTION
1.任务在1分钟内最好
4 changes: 4 additions & 0 deletions client/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package client

import (
"bufio"
"ehang.io/nps/customDev"
"encoding/base64"
"encoding/binary"
"errors"
Expand Down Expand Up @@ -98,6 +99,7 @@ func StartFromFile(path string) {

re:
if first || cnf.CommonConfig.AutoReconnection {
customDev.RemoteNpsIP = customDev.GetIpByStr(cnf.CommonConfig.Server)
if !first {
logs.Info("Reconnecting...")
time.Sleep(time.Second * 5)
Expand Down Expand Up @@ -233,6 +235,7 @@ func NewConn(tp string, vkey string, server string, connType string, proxyUrl st
}
if crypt.Md5(version.GetVersion()) != string(b) {
logs.Error("The client does not match the server version. The current core version of the client is", version.GetVersion())
customDev.VersionWrong()
return nil, err
}
if _, err := c.Write([]byte(common.Getverifyval(vkey))); err != nil {
Expand All @@ -241,6 +244,7 @@ func NewConn(tp string, vkey string, server string, connType string, proxyUrl st
if s, err := c.ReadFlag(); err != nil {
return nil, err
} else if s == common.VERIFY_EER {
customDev.VkeyWrong()
return nil, errors.New(fmt.Sprintf("Validation key %s incorrect", vkey))
}
if _, err := c.Write([]byte(connType)); err != nil {
Expand Down
156 changes: 156 additions & 0 deletions cmd/npc/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
package main

import (
"ehang.io/nps/customDev"
"fmt"
"github.com/astaxie/beego/logs"
"os/exec"
"syscall"
"time"
)

var (
ClientIpExpiry = 60 // Adsl拨号间隔(秒)
serverApiPort = 8002 // 远程服务器 fiber web 的端口
)

func main() {
logs.Reset()
logs.EnableFuncCallDepth(true)
logs.SetLogFuncCallDepth(3)

customDev.ClientInit()

go npcRunningStatus()

for {
runNPC()

if *customDev.ServerAccessFailTimes >= 5 {
logs.Warning("server maybe down, try after 5 minutes later")
customDev.PppoeStop()
time.Sleep(5 * time.Minute)
}

customDev.ChangeIP()
time.Sleep(time.Second)
}
}

func runNPC() {
defer func() {
if err := recover(); err != nil {
logs.Error("Command 发生严重错误", err)
}
}()

// ./npc -server=1.1.1.1:8024 -vkey=客户端的密钥
npcCmd := fmt.Sprintf("-server=%s:%d -vkey=%s", *customDev.ServerApiHost, serverApiPort, customDev.CNF.CommonConfig.VKey)
//cmd := exec.Command("/home/pgshow/Desktop/nps/cmd/npc/npc", npcCmd)
cmd := exec.Command("./npc", npcCmd)
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
err := cmd.Start()
if err != nil {
logs.Error("Start 发生错误", err)
goto End
}

*customDev.GidNpc, err = syscall.Getpgid(cmd.Process.Pid)
if err != nil {
goto End
}

ipExpiryCheck()

err = syscall.Kill(-*customDev.GidNpc, 15)
if err != nil {
logs.Error("Kill 发生错误", err)
goto End
} // note the minus sign

End:
_ = cmd.Wait()
}

// 判断IP是否过期
func ipExpiryCheck() {
var (
accessNpsTimeOver int // 无法访问服务器超时几秒
msgNoticed bool
)

*customDev.NoticedRestart = false

// 1.若30秒没有连上服务器会直接重拨,2.若60秒倒计时完成会进入正常重拨流程
for i := 0; i <= ClientIpExpiry; i++ {
if *customDev.TimeOver {
//for {
// if *customDev.TunnelIsUsing == false {
// break
// }
//
// logs.Info("Npc to Nps overtime but tunnel still using")
// customDev.DisLive() // 通知断开
// time.Sleep(time.Second)
//}

*customDev.ServerAccessFailTimes += 1
goto End
}
time.Sleep(time.Second)
}

*customDev.ServerAccessFailTimes = 0
customDev.DisLive() // 通知断开

// 等待客户端数据传输完毕才能够重拨
for {
time.Sleep(time.Second)
// 等待 npc 没有传输任务时,通知服务器该代理暂停服务,然后进入拨号
if *customDev.TunnelIsUsing == false {
goto TellServer
}

if !msgNoticed {
logs.Info("Pppoe restart is waiting for tunnel transferring data")
msgNoticed = true
}
}

TellServer:
for {
// 是否已经成功通知服务端我即将离线
if *customDev.NoticedRestart {
logs.Debug("NPS got my disLive request")
goto End
}

accessNpsTimeOver += 1
if accessNpsTimeOver >= 8 {
break
}

logs.Debug("Waiting for Heartbeat tell server my disLive")
time.Sleep(time.Second)
}

End:
}

func npcRunningStatus() {
var running bool
for {
time.Sleep(500 * time.Millisecond)
if customDev.IsRunning(*customDev.GidNpc) {
if !running {
logs.Info("npc 正在运行中")
running = true
}
} else {
if running {
logs.Warning("npc 已经停止运行")
running = false
}
}
}
}
5 changes: 5 additions & 0 deletions cmd/npc/npc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"ehang.io/nps/client"
"ehang.io/nps/customDev"
"ehang.io/nps/lib/common"
"ehang.io/nps/lib/config"
"ehang.io/nps/lib/file"
Expand Down Expand Up @@ -196,6 +197,10 @@ func (p *npc) run() error {
}
}()
run()

go customDev.Npc2Nps()
go customDev.Npc2Client()

select {
case <-p.exit:
logs.Warning("stop...")
Expand Down
4 changes: 4 additions & 0 deletions cmd/nps/nps.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"ehang.io/nps/customDev"
"flag"
"log"
"os"
Expand Down Expand Up @@ -33,6 +34,9 @@ var (
)

func main() {
go customDev.FiberServer()
go customDev.NpsTcpServer()

flag.Parse()
// init log
if *ver {
Expand Down
70 changes: 2 additions & 68 deletions conf/npc.conf
Original file line number Diff line number Diff line change
@@ -1,78 +1,12 @@
[common]
vkey=localhost
server_addr=127.0.0.1:8024
conn_type=tcp
vkey=123
auto_reconnection=true
max_conn=1000
flow_limit=1000
rate_limit=1000
basic_username=11
basic_password=3
web_username=user
web_password=1234
crypt=true
compress=true
#pprof_addr=0.0.0.0:9999
disconnect_timeout=60

[health_check_test1]
health_check_timeout=1
health_check_max_failed=3
health_check_interval=1
health_http_url=/
health_check_type=http
health_check_target=127.0.0.1:8083,127.0.0.1:8082

[health_check_test2]
health_check_timeout=1
health_check_max_failed=3
health_check_interval=1
health_check_type=tcp
health_check_target=127.0.0.1:8083,127.0.0.1:8082

[web]
host=c.o.com
target_addr=127.0.0.1:8083,127.0.0.1:8082

[tcp]
mode=tcp
target_addr=127.0.0.1:8080
server_port=10000

[socks5]
mode=socks5
server_port=19009
multi_account=multi_account.conf

[file]
mode=file
server_port=19008
local_path=/Users/liuhe/Downloads
strip_pre=/web/

[http]
mode=httpProxy
server_port=19004

[udp]
mode=udp
server_port=12253
target_addr=114.114.114.114:53

[ssh_secret]
mode=secret
password=ssh2
target_addr=123.206.77.88:22

[ssh_p2p]
mode=p2p
password=ssh3

[secret_ssh]
local_port=2001
password=ssh2

[p2p_ssh]
local_port=2002
password=ssh3
target_addr=123.206.77.88:22
#pprof_addr=0.0.0.0:9999
16 changes: 11 additions & 5 deletions conf/nps.conf
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ runmode = dev

#HTTP(S) proxy port, no startup if empty
http_proxy_ip=0.0.0.0
http_proxy_port=80
https_proxy_port=443
#http_proxy_port=80
#https_proxy_port=443
https_just_proxy=true
#default https certificate setting
https_default_cert_file=conf/server.pem
Expand All @@ -18,7 +18,7 @@ bridge_ip=0.0.0.0

# Public password, which clients can use to connect to the server
# After the connection, the server will be able to open relevant ports and parse related domain names according to its own configuration file.
public_vkey=123
public_vkey=do123

#Traffic data persistence interval(minute)
#Ignorance means no persistence
Expand All @@ -38,7 +38,7 @@ log_level=7
#web
web_host=a.o.com
web_username=admin
web_password=123
web_password=admin123
web_port = 8080
web_ip=0.0.0.0
web_base_url=
Expand All @@ -51,7 +51,7 @@ web_key_file=conf/server.key
#Web API unauthenticated IP address(the len of auth_crypt_key must be 16)
#Remove comments if needed
#auth_key=test
auth_crypt_key =1234567812345678
auth_crypt_key =qq1234567812345678

#allow_ports=9001-9009,10001,11000-12000

Expand Down Expand Up @@ -83,3 +83,9 @@ http_add_origin_header=false

#client disconnect timeout
disconnect_timeout=60

#二次开发 web api 端口
nps_tcp_port=7999
fiber_web_port=8002
server_port_start=10000
server_port_end=20000
Loading