Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
peakedshout committed Jun 9, 2023
1 parent 14cfb94 commit 37b8990
Show file tree
Hide file tree
Showing 30 changed files with 193 additions and 28 deletions.
Binary file modified _hook-tcp/asset/linux-386/cfc_hook_client_linux-386
Binary file not shown.
Binary file modified _hook-tcp/asset/linux-386/cfc_hook_server_linux-386
Binary file not shown.
Binary file modified _hook-tcp/asset/linux-amd64/cfc_hook_client_linux-amd64
Binary file not shown.
Binary file modified _hook-tcp/asset/linux-amd64/cfc_hook_server_linux-amd64
Binary file not shown.
Binary file modified _hook-tcp/asset/linux-arm/cfc_hook_client_linux-arm
Binary file not shown.
Binary file modified _hook-tcp/asset/linux-arm/cfc_hook_server_linux-arm
Binary file not shown.
Binary file modified _hook-tcp/asset/linux-arm64/cfc_hook_client_linux-arm64
Binary file not shown.
Binary file modified _hook-tcp/asset/linux-arm64/cfc_hook_server_linux-arm64
Binary file not shown.
Binary file modified _hook-tcp/asset/mac-amd64/cfc_hook_client_mac-amd64
Binary file not shown.
Binary file modified _hook-tcp/asset/mac-amd64/cfc_hook_server_mac-amd64
Binary file not shown.
Binary file modified _hook-tcp/asset/mac-arm64/cfc_hook_client_mac-arm64
Binary file not shown.
Binary file modified _hook-tcp/asset/mac-arm64/cfc_hook_server_mac-arm64
Binary file not shown.
Binary file modified _hook-tcp/asset/win-386/cfc_hook_client_win-386.exe
Binary file not shown.
Binary file modified _hook-tcp/asset/win-386/cfc_hook_server_win-386.exe
Binary file not shown.
Binary file modified _hook-tcp/asset/win-amd64/cfc_hook_client_win-amd64.exe
Binary file not shown.
Binary file modified _hook-tcp/asset/win-amd64/cfc_hook_server_win-amd64.exe
Binary file not shown.
Binary file modified _hook-tcp/asset/win-arm/cfc_hook_client_win-arm.exe
Binary file not shown.
Binary file modified _hook-tcp/asset/win-arm/cfc_hook_server_win-arm.exe
Binary file not shown.
Binary file modified _hook-tcp/asset/win-arm64/cfc_hook_client_win-arm64.exe
Binary file not shown.
Binary file modified _hook-tcp/asset/win-arm64/cfc_hook_server_win-arm64.exe
Binary file not shown.
11 changes: 10 additions & 1 deletion api.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (ln *Listener) Close() error {
return ln.box.Close()
}
func (ln *Listener) Addr() net.Addr {
return nil
return ln.box.LocalAddr()
}

func Listen(lname string, proxyAddr string, key string) (net.Listener, error) {
Expand All @@ -44,6 +44,11 @@ func (dl *Dialer) CallUP2P(rname string) (net.Conn, error) {
return dl.box.GetSubBoxByUP2P(rname)
}

func (dl *Dialer) CallMix(rname string, subTypes []client.SubType) (net.Conn, error) {
conn, _, err := dl.box.GetSubBoxBySubTypeMix(rname, subTypes)
return conn, err
}

func Dial(lname string, proxyAddr string, key string) (*Dialer, error) {
box, err := client.LinkProxyServer(lname, proxyAddr, key)
if err != nil {
Expand All @@ -55,3 +60,7 @@ func Dial(lname string, proxyAddr string, key string) (*Dialer, error) {
func Proxy(proxyAddr string, key string) *server.ProxyServer {
return server.NewProxyServer(proxyAddr, key)
}

func ProxyConfig(config *server.Config) *server.ProxyServer {
return server.NewProxyServer2(config)
}
45 changes: 26 additions & 19 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,25 @@ func newBox(name, addr, key string) *DeviceBox {
loger.SetLogError("ResolveTCPAddr :", err)
}
box := &DeviceBox{
name: name,
addr: tcpAddr,
conn: nil,
writeLock: sync.Mutex{},
stop: make(chan uint8, 1),
ping: tool.Ping{},
networkSpeed: tool.NewNetworkSpeedTicker(),
key: tool.NewKey(key),
taskCbCtx: nil,
subMap: sync.Map{},
subListen: nil,
subListenStop: nil,
closerOnce: sync.Once{},
name: name,
disable: atomic.Bool{},
handshake: atomic.Bool{},
addr: tcpAddr,
conn: nil,
writeLock: sync.Mutex{},
stop: make(chan uint8, 1),
ping: tool.Ping{},
networkSpeed: tool.NewNetworkSpeedTicker(),
key: tool.NewKey(key),
taskCbCtx: nil,
subMap: sync.Map{},
subMapLock: sync.Mutex{},
listenLock: sync.Mutex{},
isListen: atomic.Bool{},
subListen: nil,
subListenStop: nil,
switchListenUP2P: atomic.Bool{},
closerOnce: sync.Once{},
}
return box
}
Expand Down Expand Up @@ -86,6 +92,7 @@ func (box *DeviceBox) GetSubBox(name string) (*SubBox, error) {
}
sub := &SubBox{
id: tool.NewId(1),
subType: SubTypeProxy,
addr: nil,
key: box.key,
conn: conn,
Expand All @@ -111,16 +118,16 @@ func (box *DeviceBox) GetSubBox(name string) (*SubBox, error) {
}

func (box *DeviceBox) ListenSubBox(fn func(sub *SubBox)) error {
box.ListenLock.Lock()
box.listenLock.Lock()
if box.isListen.Load() {
err := tool.ErrBoxComplexListen
box.ListenLock.Unlock()
box.listenLock.Unlock()
loger.SetLogError(err)
} else {
box.subListen = make(chan *SubBox, 100)
box.subListenStop = make(chan error, 1)
box.isListen.Store(true)
box.ListenLock.Unlock()
box.listenLock.Unlock()
}

loger.SetLogMust(loger.SprintColor(5, 37, 37, "~~~ Start Listening SubBox ~~~"))
Expand All @@ -138,14 +145,14 @@ func (box *DeviceBox) ListenSubBox(fn func(sub *SubBox)) error {
}

func (box *DeviceBox) ListenSubBoxOnce() (sub *SubBox, err error) {
box.ListenLock.Lock()
box.listenLock.Lock()
if box.isListen.Load() {
box.ListenLock.Unlock()
box.listenLock.Unlock()
} else {
box.subListen = make(chan *SubBox, 0)
box.subListenStop = make(chan error, 0)
box.isListen.Store(true)
box.ListenLock.Unlock()
box.listenLock.Unlock()
}

select {
Expand Down
23 changes: 19 additions & 4 deletions client/client_box.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ type DeviceBox struct {
subMap sync.Map
subMapLock sync.Mutex

ListenLock sync.Mutex
listenLock sync.Mutex
isListen atomic.Bool
subListen chan *SubBox
subListenStop chan error

switchListenUP2P atomic.Bool

closerOnce sync.Once
}

Expand All @@ -55,6 +57,9 @@ func (box *DeviceBox) cMsgHandler(cMsg tool.ConnMsg) {
case tool.SOpenA:
box.listenSub(cMsg)
case tool.P2PUdpQ1:
if !box.switchListenUP2P.Load() {
return
}
box.listenUP2P(cMsg)
}
}
Expand Down Expand Up @@ -125,11 +130,9 @@ func (box *DeviceBox) listenSub(cMsg tool.ConnMsg) {
loger.SetLogMust(err)
return
}
time.Sleep(2 * time.Second)
//ln.Close()
time.Sleep(100 * time.Millisecond)
var lconn *net.TCPConn
for i := 0; i < 3; i++ {
//fmt.Println("wdwad", sub.GetRemotePublicAddr().Network(), sub.GetRemotePublicAddr().String())
pconn, err := newDialer(conn.LocalAddr(), 3*time.Second).Dial(sub.GetRemotePublicAddr().Network(), sub.GetRemotePublicAddr().String())
if err != nil {
loger.SetLogMust(err)
Expand Down Expand Up @@ -308,3 +311,15 @@ func (box *DeviceBox) GetAllNetworkSpeedView() tool.NetworkSpeedView {
list = append(list, box.GetNetworkSpeedView())
return tool.CountAllNetworkSpeedView(list...)
}

func (box *DeviceBox) SwitchListenUP2P(r bool) {
box.switchListenUP2P.Store(r)
}

func (box *DeviceBox) ProxyAddr() net.Addr {
return box.conn.RemoteAddr()
}

func (box *DeviceBox) LocalAddr() net.Addr {
return box.conn.LocalAddr()
}
2 changes: 2 additions & 0 deletions client/client_p2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"
)

// GetSubBoxByP2P tcp to p2p It's very difficult, Do not use. If you need p2p, please use GetSubBoxByUP2P
func (box *DeviceBox) GetSubBoxByP2P(name string) (*SubBox, error) {
var info tool.OdjSubOpenResp
err := box.taskCbCtx.NewTaskCbCMsg(tool.SOpenQ, 200, tool.OdjSubOpenReq{
Expand Down Expand Up @@ -41,6 +42,7 @@ func (box *DeviceBox) GetSubBoxByP2P(name string) (*SubBox, error) {
conn := rconn.(*net.TCPConn)
sub := &SubBox{
id: tool.NewId(1),
subType: SubTypeP2P,
addr: nil,
key: box.key,
conn: conn,
Expand Down
6 changes: 6 additions & 0 deletions client/client_sub.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
type SubBox struct {
id string

subType SubType

key tool.Key

conn net.Conn
Expand Down Expand Up @@ -334,3 +336,7 @@ func (sub *SubBox) WriteQueueBytes(b [][]byte) error {
}
return nil
}

func (sub *SubBox) Type() string {
return sub.subType.String()
}
41 changes: 41 additions & 0 deletions client/client_subType.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package client

import "github.com/peakedshout/go-CFC/tool"

type SubType string

const (
SubTypeUP2P = SubType("SubTypeUP2P")
SubTypeP2P = SubType("SubTypeP2P")
SubTypeProxy = SubType("SubTypeProxy")
)

func (st *SubType) String() string {
return string(*st)
}

func (box *DeviceBox) GetSubBoxBySubType(name string, subType SubType) (*SubBox, error) {
switch subType {
case SubTypeProxy:
return box.GetSubBox(name)
case SubTypeP2P:
return box.GetSubBoxByP2P(name)
case SubTypeUP2P:
return box.GetSubBoxByUP2P(name)
default:
return nil, tool.ErrSubTypeInvalid
}
}

func (box *DeviceBox) GetSubBoxBySubTypeMix(name string, subTypes []SubType) (*SubBox, []error, error) {
errs := make([]error, len(subTypes))
for i, one := range subTypes {
sub, err := box.GetSubBoxBySubType(name, one)
if err != nil {
errs[i] = err
continue
}
return sub, errs, nil
}
return nil, errs, tool.ErrSubTypeToMixGetSubBoxFailed
}
23 changes: 22 additions & 1 deletion client/client_up2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"
)

// GetSubBoxByUP2P udp to kcp
func (box *DeviceBox) GetSubBoxByUP2P(name string) (*SubBox, error) {
var info tool.OdjUP2PKId
err := box.taskCbCtx.NewTaskCbCMsg(tool.P2PUdpQ1, 200, tool.OdjUP2PKName{Name: name}).WaitCb(10*time.Second, func(cMsg tool.ConnMsg) error {
Expand Down Expand Up @@ -38,6 +39,7 @@ func (box *DeviceBox) GetSubBoxByUP2P(name string) (*SubBox, error) {

sub := &SubBox{
id: tool.NewId(1),
subType: SubTypeUP2P,
addr: si,
key: box.key,
conn: conn,
Expand Down Expand Up @@ -168,15 +170,17 @@ func (box *DeviceBox) handleUP2P(kid string, IsClient bool) (net.Conn, *tool.Sub
return nil, nil, err
}
pc2.SetDeadline(time.Time{})
kcpConnSetParam(kconn)
return kconn, info.Info, nil
} else {
ln, err := kcp.ServeConn(nil, 0, 0, pc2)
if err != nil {
box.SetWarnLog(err)
return nil, nil, err
}
kcpListenSetParam(ln)
for {
kconn, err := ln.Accept()
kconn, err := ln.AcceptKCP()
if err != nil {
box.SetWarnLog(err)
return nil, nil, err
Expand All @@ -186,6 +190,7 @@ func (box *DeviceBox) handleUP2P(kid string, IsClient bool) (net.Conn, *tool.Sub
continue
}
pc2.SetDeadline(time.Time{})
kcpConnSetParam(kconn)
return kconn, info.Info, nil
}
}
Expand Down Expand Up @@ -235,3 +240,19 @@ func newUP2PLn(addr string) (net.PacketConn, error) {
lc := net.ListenConfig{Control: control.NetControl}
return lc.ListenPacket(context.Background(), "udp", addr)
}

func kcpConnSetParam(conn *kcp.UDPSession) {
conn.SetStreamMode(true)
conn.SetWindowSize(8192, 8192)
conn.SetReadBuffer(1 * 1024 * 1024)
conn.SetWriteBuffer(1 * 1024 * 1024)
conn.SetNoDelay(0, 100, 1, 1)
conn.SetMtu(1024)
conn.SetACKNoDelay(false)
}

func kcpListenSetParam(listener *kcp.Listener) {
listener.SetReadBuffer(1 * 1024 * 1024)
listener.SetWriteBuffer(1 * 1024 * 1024)
listener.SetDSCP(46)
}
10 changes: 7 additions & 3 deletions server/proxy_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,18 @@ func (pc *ProxyClient) initLinkConn(conn net.Conn, linkType string, rf, wf tool.
pc.linkSwitch.Store(true)
lb := tool.NewLinkBox(pc.linkConn, tool.BufferSize, rf, wf)
pc.linkBox = lb
go pc.readLinkConn()
switch linkType {
case LinkTypeVPN:
go pc.readLinkConn()
case LinkTypePC:
return
}
}
func (pc *ProxyClient) writeLinkConn() error {
return pc.linkBox.WriteLinkBoxFromReader(pc.reader)
}
func (pc *ProxyClient) readLinkConn() {
defer pc.linkConn.Close()
defer pc.close()
for {
err := pc.linkBox.ReadLinkBoxToWriter(pc.rawConn, &pc.writeLock)
if err != nil {
Expand All @@ -150,7 +155,6 @@ func (pc *ProxyClient) close() {
if pc.linkConn != nil {
pc.linkConn.Close()
}
//pc.stop <- 1
pc.subMapLock.Lock()
defer pc.subMapLock.Unlock()
pc.rangeProxySubClient(func(key string, value *ProxyClient) {
Expand Down
3 changes: 3 additions & 0 deletions tool/cMsgErr.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ var ErrUnexpectedSubOpenType = errors.New("unexpected sub open type")
var ErrUnexpectedLinkConnType = errors.New("unexpected link conn type")
var ErrLinkClientIsClosed = errors.New("link client is closed")

var ErrSubTypeInvalid = errors.New("sub type invalid")
var ErrSubTypeToMixGetSubBoxFailed = errors.New("sub type to mix get sub box failed")

//var ErrHandleCMsgBad = errors.New("need one proxy client name to register")

func ErrAppend(err error, errs ...error) error {
Expand Down
Loading

0 comments on commit 37b8990

Please sign in to comment.