2019-11-09 23:02:29 +08:00
|
|
|
package goroutine
|
|
|
|
|
|
|
|
|
|
import (
|
2025-12-06 00:31:32 +08:00
|
|
|
"encoding/json"
|
2025-11-01 03:55:18 +08:00
|
|
|
"fmt"
|
2019-11-09 23:02:29 +08:00
|
|
|
"io"
|
|
|
|
|
"net"
|
2022-10-28 13:35:59 +08:00
|
|
|
"strings"
|
2019-11-09 23:02:29 +08:00
|
|
|
"sync"
|
2025-12-06 00:31:32 +08:00
|
|
|
|
|
|
|
|
"ehang.io/nps/lib/common"
|
|
|
|
|
"ehang.io/nps/lib/file"
|
2026-07-12 18:04:09 -06:00
|
|
|
"ehang.io/nps/web"
|
2025-12-06 00:31:32 +08:00
|
|
|
"github.com/astaxie/beego/logs"
|
|
|
|
|
"github.com/panjf2000/ants/v2"
|
2019-11-09 23:02:29 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type connGroup struct {
|
2022-10-29 23:59:53 +08:00
|
|
|
src io.ReadWriteCloser
|
|
|
|
|
dst io.ReadWriteCloser
|
|
|
|
|
wg *sync.WaitGroup
|
|
|
|
|
n *int64
|
|
|
|
|
flow *file.Flow
|
|
|
|
|
task *file.Tunnel
|
2026-06-05 16:45:35 -06:00
|
|
|
host *file.Host
|
2022-10-29 23:59:53 +08:00
|
|
|
remote string
|
2019-11-09 23:02:29 +08:00
|
|
|
}
|
|
|
|
|
|
2022-10-19 17:46:42 +08:00
|
|
|
//func newConnGroup(dst, src io.ReadWriteCloser, wg *sync.WaitGroup, n *int64) connGroup {
|
|
|
|
|
// return connGroup{
|
|
|
|
|
// src: src,
|
|
|
|
|
// dst: dst,
|
|
|
|
|
// wg: wg,
|
|
|
|
|
// n: n,
|
|
|
|
|
// }
|
|
|
|
|
//}
|
|
|
|
|
|
2026-06-05 16:45:35 -06:00
|
|
|
func newConnGroup(dst, src io.ReadWriteCloser, wg *sync.WaitGroup, n *int64, flow *file.Flow, task *file.Tunnel, host *file.Host, remote string) connGroup {
|
2019-11-09 23:02:29 +08:00
|
|
|
return connGroup{
|
2022-10-29 23:59:53 +08:00
|
|
|
src: src,
|
|
|
|
|
dst: dst,
|
|
|
|
|
wg: wg,
|
|
|
|
|
n: n,
|
|
|
|
|
flow: flow,
|
|
|
|
|
task: task,
|
2026-06-05 16:45:35 -06:00
|
|
|
host: host,
|
2022-10-29 23:59:53 +08:00
|
|
|
remote: remote,
|
2019-11-09 23:02:29 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-05 16:45:35 -06:00
|
|
|
func CopyBuffer(dst io.Writer, src io.Reader, flow *file.Flow, task *file.Tunnel, host *file.Host, remote string) (err error) {
|
2022-10-19 17:46:42 +08:00
|
|
|
buf := common.CopyBuff.Get()
|
|
|
|
|
defer common.CopyBuff.Put(buf)
|
|
|
|
|
for {
|
2022-11-04 14:53:34 +08:00
|
|
|
if len(buf) <= 0 {
|
|
|
|
|
break
|
|
|
|
|
}
|
2022-10-19 17:46:42 +08:00
|
|
|
nr, er := src.Read(buf)
|
2022-11-04 14:53:34 +08:00
|
|
|
|
|
|
|
|
if task != nil {
|
2025-11-01 03:55:18 +08:00
|
|
|
if task.Client.IpWhite && task.Client.IpWhitePass != "" {
|
2025-12-06 00:31:32 +08:00
|
|
|
|
2025-11-01 03:55:18 +08:00
|
|
|
if common.IsAuthIp(remote, task.Client.VerifyKey, task.Client.IpWhiteList) {
|
2025-12-06 00:31:32 +08:00
|
|
|
ip := common.GetIpByAddr(remote)
|
|
|
|
|
var jsonBytes []byte
|
|
|
|
|
|
2026-07-12 18:04:09 -06:00
|
|
|
errorContent, _ := web.ReadStaticFile("page/auth.html")
|
2025-11-01 03:55:18 +08:00
|
|
|
authHtml := string(errorContent)
|
2025-12-06 00:31:32 +08:00
|
|
|
authHtml = strings.ReplaceAll(authHtml, "${ip}", ip)
|
|
|
|
|
|
|
|
|
|
fullRequest := string(buf[0:nr])
|
|
|
|
|
// 获取HTTP请求的第一行
|
|
|
|
|
lines := strings.Split(fullRequest, "\r\n")
|
|
|
|
|
if len(lines) == 0 {
|
|
|
|
|
lines = strings.Split(fullRequest, "\n")
|
|
|
|
|
}
|
|
|
|
|
firstLine := lines[0]
|
|
|
|
|
|
2025-12-06 01:50:13 +08:00
|
|
|
// 优先处理客户端直接访问的 POST /authIp 请求,直接响应给客户端,不经隧道转发
|
|
|
|
|
if strings.HasPrefix(firstLine, "POST /authIp") {
|
|
|
|
|
pass := ""
|
|
|
|
|
parts := strings.Split(firstLine, " ")
|
|
|
|
|
if len(parts) > 1 {
|
|
|
|
|
path := parts[1]
|
|
|
|
|
if strings.Contains(path, "/authIp?pass=") {
|
|
|
|
|
pass = strings.ReplaceAll(path, "/authIp?pass=", "")
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-12-06 00:31:32 +08:00
|
|
|
if pass == task.Client.IpWhitePass {
|
|
|
|
|
task.Client.IpWhiteList = append(task.Client.IpWhiteList, ip)
|
|
|
|
|
file.GetDb().UpdateClient(task.Client)
|
|
|
|
|
logs.Info("客户端IP白名单认证授权成功:vkey [%s] ip [%s] password [%s]", task.Client.VerifyKey, ip, pass)
|
|
|
|
|
jsonBytes, err = json.Marshal(map[string]interface{}{"success": true, "message": "授权成功"})
|
|
|
|
|
} else {
|
|
|
|
|
logs.Error("客户端IP白名单认证授权密码错误:vkey [%s] ip [%s] password [%s]", task.Client.VerifyKey, ip, pass)
|
|
|
|
|
jsonBytes, err = json.Marshal(map[string]interface{}{"success": false, "message": "密码错误"})
|
|
|
|
|
}
|
|
|
|
|
response := fmt.Sprintf("HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: %d\r\nConnection: close\r\n\r\n%s", len(jsonBytes), jsonBytes)
|
2025-12-06 01:50:13 +08:00
|
|
|
// 如果 src 是真实的客户端连接(net.Conn),直接写回客户端并关闭连接,避免走隧道转发
|
|
|
|
|
if connSrc, ok := src.(net.Conn); ok {
|
|
|
|
|
connSrc.Write([]byte(response))
|
|
|
|
|
connSrc.Close()
|
|
|
|
|
} else {
|
|
|
|
|
dst.Write([]byte(response))
|
|
|
|
|
}
|
2025-12-06 00:31:32 +08:00
|
|
|
return
|
|
|
|
|
}
|
2022-10-29 23:59:53 +08:00
|
|
|
|
2025-12-06 01:50:13 +08:00
|
|
|
// 非授权IP,返回授权页面(同样优先返回给客户端)
|
2025-11-01 03:55:18 +08:00
|
|
|
response := fmt.Sprintf("HTTP/1.1 401 Unauthorized\r\nContent-Type: text/html; charset=utf-8\r\nContent-Length: %d\r\nConnection: close\r\n\r\n%s", len(authHtml), authHtml)
|
2025-12-06 01:50:13 +08:00
|
|
|
if connSrc, ok := src.(net.Conn); ok {
|
|
|
|
|
connSrc.Write([]byte(response))
|
|
|
|
|
connSrc.Close()
|
|
|
|
|
} else {
|
|
|
|
|
dst.Write([]byte(response))
|
|
|
|
|
}
|
2025-12-06 00:31:32 +08:00
|
|
|
return
|
2022-10-28 13:35:59 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-19 17:46:42 +08:00
|
|
|
if nr > 0 {
|
|
|
|
|
nw, ew := dst.Write(buf[0:nr])
|
|
|
|
|
if nw > 0 {
|
|
|
|
|
//written += int64(nw)
|
2022-11-01 14:35:13 +08:00
|
|
|
if flow != nil {
|
|
|
|
|
flow.Add(int64(nw), int64(nw))
|
|
|
|
|
// <<20 = 1024 * 1024
|
|
|
|
|
if flow.FlowLimit > 0 && (flow.FlowLimit<<20) < (flow.ExportFlow+flow.InletFlow) {
|
2025-12-06 00:31:32 +08:00
|
|
|
logs.Error("隧道[%s]流量已经超出", task.Client.VerifyKey)
|
2022-11-01 14:35:13 +08:00
|
|
|
break
|
|
|
|
|
}
|
2022-10-19 17:46:42 +08:00
|
|
|
}
|
2026-06-05 16:45:35 -06:00
|
|
|
if task != nil && task.Flow != nil && task.Flow != flow {
|
|
|
|
|
task.Flow.Add(int64(nw), int64(nw))
|
|
|
|
|
}
|
|
|
|
|
if host != nil && host.Flow != nil && host.Flow != flow {
|
|
|
|
|
host.Flow.Add(int64(nw), int64(nw))
|
|
|
|
|
}
|
2022-10-19 17:46:42 +08:00
|
|
|
}
|
|
|
|
|
if ew != nil {
|
|
|
|
|
err = ew
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
if nr != nw {
|
|
|
|
|
err = io.ErrShortWrite
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if er != nil {
|
|
|
|
|
err = er
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-09 23:02:29 +08:00
|
|
|
func copyConnGroup(group interface{}) {
|
2022-10-19 17:46:42 +08:00
|
|
|
//logs.Info("copyConnGroup.........")
|
2019-11-09 23:02:29 +08:00
|
|
|
cg, ok := group.(connGroup)
|
|
|
|
|
if !ok {
|
|
|
|
|
return
|
|
|
|
|
}
|
2025-12-06 00:31:32 +08:00
|
|
|
|
2019-11-09 23:02:29 +08:00
|
|
|
var err error
|
2026-06-05 16:45:35 -06:00
|
|
|
err = CopyBuffer(cg.dst, cg.src, cg.flow, cg.task, cg.host, cg.remote)
|
2019-11-09 23:02:29 +08:00
|
|
|
if err != nil {
|
|
|
|
|
cg.src.Close()
|
|
|
|
|
cg.dst.Close()
|
|
|
|
|
//logs.Warn("close npc by copy from nps", err, c.connId)
|
|
|
|
|
}
|
2022-10-19 17:46:42 +08:00
|
|
|
|
|
|
|
|
//if conns.flow != nil {
|
|
|
|
|
// conns.flow.Add(in, out)
|
|
|
|
|
//}
|
2019-11-09 23:02:29 +08:00
|
|
|
cg.wg.Done()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Conns struct {
|
|
|
|
|
conn1 io.ReadWriteCloser // mux connection
|
|
|
|
|
conn2 net.Conn // outside connection
|
|
|
|
|
flow *file.Flow
|
2020-02-09 00:44:09 +08:00
|
|
|
wg *sync.WaitGroup
|
2022-10-28 18:01:21 +08:00
|
|
|
task *file.Tunnel
|
2026-06-05 16:45:35 -06:00
|
|
|
host *file.Host
|
2019-11-09 23:02:29 +08:00
|
|
|
}
|
|
|
|
|
|
2026-06-05 16:45:35 -06:00
|
|
|
func NewConns(c1 io.ReadWriteCloser, c2 net.Conn, flow *file.Flow, wg *sync.WaitGroup, task *file.Tunnel, host *file.Host) Conns {
|
2019-11-09 23:02:29 +08:00
|
|
|
return Conns{
|
|
|
|
|
conn1: c1,
|
|
|
|
|
conn2: c2,
|
|
|
|
|
flow: flow,
|
2020-02-09 00:44:09 +08:00
|
|
|
wg: wg,
|
2022-10-28 18:01:21 +08:00
|
|
|
task: task,
|
2026-06-05 16:45:35 -06:00
|
|
|
host: host,
|
2019-11-09 23:02:29 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func copyConns(group interface{}) {
|
2022-10-19 17:46:42 +08:00
|
|
|
//logs.Info("copyConns.........")
|
2019-11-09 23:02:29 +08:00
|
|
|
conns := group.(Conns)
|
|
|
|
|
wg := new(sync.WaitGroup)
|
|
|
|
|
wg.Add(2)
|
|
|
|
|
var in, out int64
|
2022-10-29 23:59:53 +08:00
|
|
|
remoteAddr := conns.conn2.RemoteAddr().String()
|
2026-06-05 16:45:35 -06:00
|
|
|
_ = connCopyPool.Invoke(newConnGroup(conns.conn1, conns.conn2, wg, &in, conns.flow, conns.task, conns.host, remoteAddr))
|
2019-11-09 23:02:29 +08:00
|
|
|
// outside to mux : incoming
|
2026-06-05 16:45:35 -06:00
|
|
|
_ = connCopyPool.Invoke(newConnGroup(conns.conn2, conns.conn1, wg, &out, conns.flow, conns.task, conns.host, remoteAddr))
|
2019-11-09 23:02:29 +08:00
|
|
|
// mux to outside : outgoing
|
|
|
|
|
wg.Wait()
|
2022-10-19 17:46:42 +08:00
|
|
|
//if conns.flow != nil {
|
|
|
|
|
// conns.flow.Add(in, out)
|
|
|
|
|
//}
|
2020-02-09 00:44:09 +08:00
|
|
|
conns.wg.Done()
|
2019-11-09 23:02:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var connCopyPool, _ = ants.NewPoolWithFunc(200000, copyConnGroup, ants.WithNonblocking(false))
|
|
|
|
|
var CopyConnsPool, _ = ants.NewPoolWithFunc(100000, copyConns, ants.WithNonblocking(false))
|