Skip to content

Commit

Permalink
Merge pull request #298 from cnlh/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
ffdfgdfg authored Dec 9, 2019
2 parents f97a917 + 999fac8 commit 2f039ac
Show file tree
Hide file tree
Showing 15 changed files with 350 additions and 69 deletions.
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,23 +247,22 @@ nps是一款轻量级、高性能、功能强大的**内网穿透**代理服务
**适用范围:** 大流量传输场景,流量不经过公网服务器,但是由于p2p穿透和nat类型关系较大,不保证100%成功,支持大部分nat类型。[nat类型检测](#nat类型检测)

**假设场景:**
内网1机器ip为10.1.50.2 内网2机器2 ip为10.2.50.2

想通过访问内网1机器1的2000端口---->访问到内网2机器3 10.2.50.3的22端口
想通过访问使用端机器(访问端,也就是本机)的2000端口---->访问到内网机器 10.2.50.2的22端口

**使用步骤**
-`nps.conf`中设置`p2p_ip`(nps服务器ip)和`p2p_port`(nps服务器udp端口)
- 在刚才刚才创建的客户端中添加一条p2p代理,并设置唯一密钥p2pssh
- 在机器1执行命令
- 在使用端机器(本机)执行命令

```
./npc -server=1.1.1.1:8284 -vkey=123 -password=p2pssh -target=10.2.50.3:22
./npc -server=1.1.1.1:8284 -vkey=123 -password=p2pssh -target=10.2.50.2:22
```
如需指定本地端口可加参数`-local_port=xx`,默认为2000

**注意:** password为web管理上添加的唯一密钥,具体命令可查看web管理上的命令提示

假设机器3用户名为root,现在在机器1上执行`ssh -p 2000 [email protected]`即可访问机器2的ssh
假设内网机器为10.2.50.2的ssh用户名为root,现在在本机上执行`ssh -p 2000 [email protected]`即可访问机器2的ssh,如果是网站在浏览器访问127.0.0.1:2000端口即可。



Expand Down Expand Up @@ -1040,6 +1039,13 @@ POST /auth/getauthkey

## 捐助
如果您觉得nps对你有帮助,欢迎给予我们一定捐助,也是帮助nps更好的发展。
## 致谢
Thanks [jetbrains](https://www.jetbrains.com/?from=nps) for providing development tools for nps

<html>
<img src="https://ftp.bmp.ovh/imgs/2019/12/6435398b0c7402b1.png" width="300" align=center />
</html>


### 支付宝
![image](https://github.com/cnlh/nps/blob/master/image/donation_zfb.png?raw=true)
Expand Down
34 changes: 17 additions & 17 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#/bash/sh
export VERSION=0.25.1

sudo apt-get install gcc-mingw-w64-i686
env GOOS=windows GOARCH=386 CGO_ENABLED=1 CC=i686-w64-mingw32-gcc go build -ldflags "-s -w -extldflags -static -extldflags -static" -buildmode=c-shared -o npc_sdk.dll cmd/npc/sdk.go
Expand All @@ -10,75 +11,75 @@ cp upx-3.95-amd64_linux/upx ./

CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/npc/npc.go

tar -czvf linux_amd64_client.tar.gz npc conf/npc.conf
tar -czvf linux_amd64_client.tar.gz npc conf/npc.conf conf/multi_account.conf

CGO_ENABLED=0 GOOS=linux GOARCH=386 go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/npc/npc.go

tar -czvf linux_386_client.tar.gz npc conf/npc.conf
tar -czvf linux_386_client.tar.gz npc conf/npc.conf conf/multi_account.conf

CGO_ENABLED=0 GOOS=freebsd GOARCH=386 go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/npc/npc.go

tar -czvf freebsd_386_client.tar.gz npc conf/npc.conf
tar -czvf freebsd_386_client.tar.gz npc conf/npc.conf conf/multi_account.conf

CGO_ENABLED=0 GOOS=freebsd GOARCH=amd64 go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/npc/npc.go

tar -czvf freebsd_amd64_client.tar.gz npc conf/npc.conf
tar -czvf freebsd_amd64_client.tar.gz npc conf/npc.conf conf/multi_account.conf

CGO_ENABLED=0 GOOS=freebsd GOARCH=arm go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/npc/npc.go

tar -czvf freebsd_arm_client.tar.gz npc conf/npc.conf
tar -czvf freebsd_arm_client.tar.gz npc conf/npc.conf conf/multi_account.conf

CGO_ENABLED=0 GOOS=linux GOARCH=arm GOARM=7 go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/npc/npc.go

tar -czvf linux_arm_v7_client.tar.gz npc conf/npc.conf
tar -czvf linux_arm_v7_client.tar.gz npc conf/npc.conf conf/multi_account.conf

CGO_ENABLED=0 GOOS=linux GOARCH=arm GOARM=6 go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/npc/npc.go

tar -czvf linux_arm_v6_client.tar.gz npc conf/npc.conf
tar -czvf linux_arm_v6_client.tar.gz npc conf/npc.conf conf/multi_account.conf

CGO_ENABLED=0 GOOS=linux GOARCH=arm GOARM=5 go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/npc/npc.go

tar -czvf linux_arm_v5_client.tar.gz npc conf/npc.conf
tar -czvf linux_arm_v5_client.tar.gz npc conf/npc.conf conf/multi_account.conf


CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/npc/npc.go

tar -czvf linux_arm64_client.tar.gz npc conf/npc.conf
tar -czvf linux_arm64_client.tar.gz npc conf/npc.conf conf/multi_account.conf


CGO_ENABLED=0 GOOS=linux GOARCH=mips64 go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/npc/npc.go

tar -czvf linux_mips64_client.tar.gz npc conf/npc.conf
tar -czvf linux_mips64_client.tar.gz npc conf/npc.conf conf/multi_account.conf


CGO_ENABLED=0 GOOS=linux GOARCH=mips64le go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/npc/npc.go

tar -czvf linux_mips64le_client.tar.gz npc conf/npc.conf
tar -czvf linux_mips64le_client.tar.gz npc conf/npc.conf conf/multi_account.conf


CGO_ENABLED=0 GOOS=linux GOARCH=mipsle go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/npc/npc.go

tar -czvf linux_mipsle_client.tar.gz npc conf/npc.conf
tar -czvf linux_mipsle_client.tar.gz npc conf/npc.conf conf/multi_account.conf


CGO_ENABLED=0 GOOS=linux GOARCH=mips go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/npc/npc.go

tar -czvf linux_mips_client.tar.gz npc conf/npc.conf
tar -czvf linux_mips_client.tar.gz npc conf/npc.conf conf/multi_account.conf


CGO_ENABLED=0 GOOS=windows GOARCH=386 go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/npc/npc.go

tar -czvf win_386_client.tar.gz npc.exe conf/npc.conf
tar -czvf win_386_client.tar.gz npc.exe conf/npc.conf conf/multi_account.conf


CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/npc/npc.go

tar -czvf win_amd64_client.tar.gz npc.exe conf/npc.conf
tar -czvf win_amd64_client.tar.gz npc.exe conf/npc.conf conf/multi_account.conf


CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/npc/npc.go

tar -czvf macos_client.tar.gz npc conf/npc.conf
tar -czvf macos_client.tar.gz npc conf/npc.conf conf/multi_account.conf

CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/nps/nps.go

Expand Down Expand Up @@ -157,7 +158,6 @@ CGO_ENABLED=0 GOOS=windows GOARCH=386 go build -ldflags "-s -w -extldflags -stat

tar -czvf win_386_server.tar.gz conf/nps.conf conf/tasks.json conf/clients.json conf/hosts.json conf/server.key conf/server.pem web/views web/static nps.exe

export VERSION=0.25.0
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt-get update
Expand Down
12 changes: 11 additions & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,19 @@ func NewRPClient(svraddr string, vKey string, bridgeConnType string, proxyUrl st
}
}

var NowStatus int
var CloseClient bool
//start
func (s *TRPClient) Start() {
CloseClient = false
retry:
if CloseClient {
return
}
NowStatus = 0
c, err := NewConn(s.bridgeConnType, s.vKey, s.svrAddr, common.WORK_MAIN, s.proxyUrl)
if err != nil {
logs.Error("The connection server failed and will be reconnected in five seconds")
logs.Error("The connection server failed and will be reconnected in five seconds, error", err.Error())
time.Sleep(time.Second * 5)
goto retry
}
Expand All @@ -66,6 +73,7 @@ retry:
if s.cnf != nil && len(s.cnf.Healths) > 0 {
go heathCheck(s.cnf.Healths, s.signal)
}
NowStatus = 1
//msg connection, eg udp
s.handleMain()
}
Expand Down Expand Up @@ -279,6 +287,8 @@ loop:
}

func (s *TRPClient) Close() {
CloseClient = true
NowStatus = 0
if s.tunnel != nil {
s.tunnel.Close()
}
Expand Down
7 changes: 3 additions & 4 deletions client/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func NewConn(tp string, vkey string, server string, connType string, proxyUrl st
return nil, er
}
connection, err = n.Dial("tcp", server)
case "http":
default:
connection, err = NewHttpProxyConn(u, server)
}
} else {
Expand Down Expand Up @@ -241,8 +241,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 {
logs.Error("Validation key %s incorrect", vkey)
os.Exit(0)
return nil, errors.New(fmt.Sprintf("Validation key %s incorrect", vkey))
}
if _, err := c.Write([]byte(connType)); err != nil {
return nil, err
Expand All @@ -262,7 +261,7 @@ func NewHttpProxyConn(url *url.URL, remoteAddr string) (net.Conn, error) {
Proto: "HTTP/1.1",
}
password, _ := url.User.Password()
req.Header.Set("Proxy-Authorization", "Basic "+basicAuth(url.User.Username(), password))
req.Header.Set("Authorization", "Basic "+basicAuth(strings.Trim(url.User.Username(), " "), password))
b, err := httputil.DumpRequest(req, false)
if err != nil {
return nil, err
Expand Down
42 changes: 20 additions & 22 deletions cmd/npc/sdk.go
Original file line number Diff line number Diff line change
@@ -1,51 +1,49 @@
package main

import "C"
import (
"C"
"github.com/astaxie/beego/logs"
"github.com/cnlh/nps/client"
"time"
"github.com/cnlh/nps/lib/common"
"github.com/cnlh/nps/lib/version"
)

func init() {
logs.SetLogger(logs.AdapterFile, `{"filename":"npc.log","daily":false,"maxlines":100000,"color":true}`)
}

var status int
var closeBefore int
var cl *client.TRPClient

//export StartClientByVerifyKey
func StartClientByVerifyKey(serverAddr, verifyKey, connType, proxyUrl *C.char) int {
logs.SetLogger("store")
if cl != nil {
closeBefore = 1
cl.Close()
}
cl = client.NewRPClient(C.GoString(serverAddr), C.GoString(verifyKey), C.GoString(connType), C.GoString(proxyUrl), nil)
closeBefore = 0
go func() {
for {
status = 1
cl.Start()
status = 0
if closeBefore == 1 {
return
}
time.Sleep(time.Second * 5)
}
cl.Start()
return
}()
return 1
}

//export GetClientStatus
func GetClientStatus() int {
return status
return client.NowStatus
}

//export CloseClient
func CloseClient() {
closeBefore = 1
cl.Close()
if cl != nil {
cl.Close()
}
}

//export Version
func Version() *C.char {
return C.CString(version.VERSION)
}

//export Logs
func Logs() *C.char {
return C.CString(common.GetLogMsg())
}

func main() {
Expand Down
48 changes: 48 additions & 0 deletions lib/common/logs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package common

import (
"github.com/astaxie/beego/logs"
"time"
)

const MaxMsgLen = 5000

var logMsgs string

func init() {
logs.Register("store", func() logs.Logger {
return new(StoreMsg)
})
}

func GetLogMsg() string {
return logMsgs
}

type StoreMsg struct {
}

func (lg *StoreMsg) Init(config string) error {
return nil
}

func (lg *StoreMsg) WriteMsg(when time.Time, msg string, level int) error {
m := when.Format("2006-01-02 15:04:05") + " " + msg + "\r\n"
if len(logMsgs) > MaxMsgLen {
start := MaxMsgLen - len(m)
if start <= 0 {
start = MaxMsgLen
}
logMsgs = logMsgs[start:]
}
logMsgs += m
return nil
}

func (lg *StoreMsg) Destroy() {
return
}

func (lg *StoreMsg) Flush() {
return
}
12 changes: 7 additions & 5 deletions lib/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,13 +241,15 @@ func dealTunnel(s string) *file.Tunnel {
t.StripPre = item[1]
case "multi_account":
t.MultiAccount = &file.MultiAccount{}
if b, err := common.ReadAllFromFile(item[1]); err != nil {
panic(err)
} else {
if content, err := common.ParseStr(string(b)); err != nil {
if common.FileExists(item[1]){
if b, err := common.ReadAllFromFile(item[1]); err != nil {
panic(err)
} else {
t.MultiAccount.AccountMap = dealMultiUser(content)
if content, err := common.ParseStr(string(b)); err != nil {
panic(err)
} else {
t.MultiAccount.AccountMap = dealMultiUser(content)
}
}
}
}
Expand Down
19 changes: 15 additions & 4 deletions lib/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,22 @@ WantedBy=multi-user.target`
log.Println("Executable files have been copied to", "/usr/bin/nps")
}
systemd := unit + "\n\n" + service + "\n\n" + install
_ = os.Remove("/usr/lib/systemd/system/nps.service")
err := ioutil.WriteFile("/usr/lib/systemd/system/nps.service", []byte(systemd), 0644)
if err != nil {
log.Println("Write systemd service err ", err)
if _, err := os.Stat("/usr/lib/systemd/system"); os.IsExist(err) {
_ = os.Remove("/usr/lib/systemd/system/nps.service")
err := ioutil.WriteFile("/usr/lib/systemd/system/nps.service", []byte(systemd), 0644)
if err != nil {
log.Println("Write systemd service err ", err)
}
} else if _, err := os.Stat("/lib/systemd/system"); os.IsExist(err) {
_ = os.Remove("/lib/systemd/system/nps.service")
err := ioutil.WriteFile("/lib/systemd/system/nps.service", []byte(systemd), 0644)
if err != nil {
log.Println("Write systemd service err ", err)
}
} else {
log.Println("Write systemd service fail, not found the systemd system path ")
}

_ = os.Mkdir("/var/log/nps", 644)
}
log.Println("install ok!")
Expand Down
Loading

0 comments on commit 2f039ac

Please sign in to comment.