diff --git a/README.md b/README.md index 472ec9f..3d3bd73 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,3 @@ -# 抗议中兴公司拿别人的开源软件XXL-JOB,申请专利的行为,这样的流氓、小偷公司,请大家以后拒绝这种公司提供的一切产品,维护开源产业。 # port-forward Go语言开发的端口转发工具 for port data forward diff --git a/forward-core/README.md b/forward-core/README.md index e3a8abc..0f3b190 100644 --- a/forward-core/README.md +++ b/forward-core/README.md @@ -1,2 +1,2 @@ -# No Commit +# 公共类库 diff --git a/forward-core/common/NetCommon.go b/forward-core/common/NetCommon.go new file mode 100644 index 0000000..278194f --- /dev/null +++ b/forward-core/common/NetCommon.go @@ -0,0 +1,109 @@ +package common + +import ( + "bufio" + "encoding/binary" + "errors" + "net" + + "github.com/astaxie/beego/logs" +) + +type NetCommon struct { +} + +type ReadCallBack func(conn net.Conn, id int, cmd byte, arg []byte) + +const ( + MagicAuthFail = byte(iota) + DoAuth + MagicRefused + AgentListenerOpen + AgentConnOpen + AgentConnClose + MsgToMagic + MsgToAgent +) + +func (_self *NetCommon) ReadConn(conn net.Conn, callback ReadCallBack) { + scanner := bufio.NewScanner(conn) + scanner.Split(func(data []byte, atEOF bool) (adv int, token []byte, err error) { + return _self.Split(data, atEOF, conn, callback) + }) + for scanner.Scan() { + } + if scanner.Err() != nil { + logs.Error(scanner.Err()) + } +} + +func (_self *NetCommon) Split(data []byte, atEOF bool, conn net.Conn, callback ReadCallBack) (adv int, token []byte, err error) { + l := len(data) + if l < 6 { + return 0, nil, nil + } + if l > 100000 { + conn.Close() + return 0, nil, errors.New("max data") + } + var id int + var cmd byte + id = int(int32(data[0]) | int32(data[1])<<8 | int32(data[2])<<16 | int32(data[3])<<24) + cmd = data[4] + isShort := data[5] + var payload []byte + var offset int + if isShort == 1 { + offset = 6 + } else { + if l < 10 { + return 0, nil, nil + } + ls := binary.LittleEndian.Uint32(data[6:]) + tail := l - 10 + if tail < int(ls) { + return 0, nil, nil + } + payload = data[10 : 10+ls] + offset = 10 + int(ls) + } + + callback(conn, id, cmd, payload) + + return offset, []byte{}, nil +} + +func (_self *NetCommon) WriteConn(conn net.Conn, id int, cmd byte, payload []byte) error { + if conn == nil { + return nil + } + l := len(payload) + var buf []byte + var size int + if l > 0 { + size = 10 + l + //4+1+1+4 id cmd isShort + buf = make([]byte, size) + } else { + size = 6 + //4+1+1 id cmd isShort + buf = make([]byte, size) + } + buf[0] = byte(id) + buf[1] = byte(id >> 8) + buf[2] = byte(id >> 16) + buf[3] = byte(id >> 24) + buf[4] = cmd + if l > 0 { + buf[5] = 0 + binary.LittleEndian.PutUint32(buf[6:], uint32(l)) + copy(buf[10:], []byte(payload)) + } else { + buf[5] = 1 + } + _, err := conn.Write(buf[:size]) + if err != nil { + logs.Error("conn.Write error:", err) + } + return err +} diff --git a/net-agent/README.md b/net-agent/README.md index e3a8abc..6b0ca0b 100644 --- a/net-agent/README.md +++ b/net-agent/README.md @@ -1,2 +1,3 @@ # No Commit +抱歉,net-agent暂不开源,请关注后续更新