Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
peakedshout committed May 9, 2023
1 parent 1a2327e commit 0c1d070
Show file tree
Hide file tree
Showing 19 changed files with 1,061 additions and 213 deletions.
3 changes: 2 additions & 1 deletion _hook-tcp/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ func runServer() {
loger.SetLoggerLevel(c.Setting.LogLevel)
loger.SetLoggerStack(c.Setting.LogStack)

err := tool.ReRun(c.Setting.ReLinkTime, func() {
err := tool.ReRun(c.Setting.ReLinkTime, func() bool {
server.NewProxyServer(c.ProxyServerHost.ProxyServerAddr, c.ProxyServerHost.LinkProxyKey).Wait()
return true
})
if err != nil {
loger.SetLogError(err)
Expand Down
52 changes: 52 additions & 0 deletions api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package cfc

import (
"github.com/peakedshout/go-CFC/client"
"github.com/peakedshout/go-CFC/server"
"net"
)

type Listener struct {
box *client.DeviceBox
}

func (ln *Listener) Accept() (conn net.Conn, err error) {
return ln.box.ListenSubBoxOnce()
}
func (ln *Listener) Close() error {
return ln.box.Close()
}
func (ln *Listener) Addr() net.Addr {
return nil
}

func Listen(lname string, proxyAddr string, key string) (net.Listener, error) {
box, err := client.LinkProxyServer(lname, proxyAddr, key)
if err != nil {
return nil, err
}
return &Listener{box: box}, nil
}

type Dialer struct {
box *client.DeviceBox
}

func (dl *Dialer) Close() error {
return dl.box.Close()
}

func (dl *Dialer) Call(rname string) (net.Conn, error) {
return dl.box.GetSubBox(rname)
}
func Dial(lname string, proxyAddr string, key string) (*Dialer, error) {
box, err := client.LinkProxyServer(lname, proxyAddr, key)
if err != nil {
return nil, err
}
return &Dialer{box: box}, nil
}

func Proxy(proxyAddr string, key string) *server.ProxyServer {
return server.NewProxyServer(proxyAddr, key)
}
43 changes: 36 additions & 7 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,27 +112,52 @@ func (box *DeviceBox) GetSubBox(name string) (*SubBox, error) {
}

func (box *DeviceBox) ListenSubBox(fn func(sub *SubBox)) error {
if box.subListen != nil || box.subListenStop != nil {
box.ListenLock.Lock()
if box.isListen.Load() {
err := tool.ErrBoxComplexListen
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.subListen = make(chan *SubBox, 100)
box.subListenStop = make(chan error, 1)

//box.SetInfoLog("~~~ Start Listening SubBox ~~~")
loger.SetLogMust(loger.SprintColor(5, 37, 37, "~~~ Start Listening SubBox ~~~"))
for {
select {
case sub := <-box.subListen:
go fn(sub)
case err := <-box.subListenStop:
box.subListenStop <- err
err = tool.ErrAppend(tool.ErrBoxStopListen, err)
box.SetWarnLog(err)
return err
}
}
}

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

select {
case sub = <-box.subListen:
return sub, nil
case err = <-box.subListenStop:
err = tool.ErrAppend(tool.ErrBoxStopListen, err)
return nil, err
}
}

func (box *DeviceBox) GetOtherDelayPing(name ...string) ([]tool.OdjPing, error) {
var resp []tool.OdjPing
err := box.taskCbCtx.NewTaskCbCMsg(tool.DelayQ, 200, tool.OdjIdList{IdList: name}).WaitCb(10*time.Second, func(cMsg tool.ConnMsg) error {
Expand Down Expand Up @@ -161,21 +186,25 @@ func (box *DeviceBox) GetOtherDelayPing(name ...string) ([]tool.OdjPing, error)
return resp, nil
}

func (box *DeviceBox) Close() {
func (box *DeviceBox) Close() error {
if box.conn == nil {
return tool.ErrConnIsNil
}
err := box.conn.Close()
box.closerOnce.Do(func() {
box.conn.Close()
box.stop <- 1
box.subMapLock.Lock()
defer box.subMapLock.Unlock()
box.disable.Store(true)
if box.subListenStop != nil {
if box.isListen.Load() {
box.subListenStop <- tool.ErrBoxIsClosed
}
box.rangeProxySubClient(func(key string, value *SubBox) {
value.Close()
})
loger.SetLogMust(loger.SprintColor(5, 37, 37, "~~~ Closed Proxy Box ~~~"))
})
return err
}

func (box *DeviceBox) Wait() {
Expand Down
10 changes: 6 additions & 4 deletions client/client_box.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package client

import (
"context"
"fmt"
"github.com/peakedshout/go-CFC/loger"
"github.com/peakedshout/go-CFC/tool"
"net"
Expand All @@ -20,7 +19,7 @@ type DeviceBox struct {

conn *net.TCPConn
writeLock sync.Mutex
//writeChan chan [][]byte

stop chan uint8
ping tool.Ping
networkSpeed tool.NetworkSpeedTicker
Expand All @@ -32,6 +31,8 @@ type DeviceBox struct {
subMap sync.Map
subMapLock sync.Mutex

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

Expand Down Expand Up @@ -127,7 +128,7 @@ func (box *DeviceBox) listenSub(cMsg tool.ConnMsg) {
//ln.Close()
var lconn *net.TCPConn
for i := 0; i < 3; i++ {
fmt.Println("wdwad", sub.GetRemotePublicAddr().Network(), sub.GetRemotePublicAddr().String())
//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 All @@ -152,7 +153,8 @@ func (box *DeviceBox) listenSub(cMsg tool.ConnMsg) {
select {
case box.subListen <- sub:
box.setSubBox(sub.id, sub)
default:
case err := <-box.subListenStop:
box.subListenStop <- err
sub.Close()
}
}
Expand Down
1 change: 1 addition & 0 deletions client/client_sub.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ func (sub *SubBox) Close() error {
errList = append(errList, value.Close())
})
err = tool.ErrAppend(err, errList...)
sub.root.delSubBox(sub.id)
sub.SetInfoLog("is closed")
})
return err
Expand Down
Loading

0 comments on commit 0c1d070

Please sign in to comment.