| // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | package msg |
| 16 |
| 17 | import ( |
| 18 | "encoding/binary" |
| 19 | "encoding/json" |
| 20 | "fmt" |
| 21 | "io" |
| 22 | "reflect" |
| 23 |
| 24 | "github.com/fatedier/frp/pkg/proto/wire" |
| 25 | ) |
| 26 |
| 27 | const ( |
| 28 | V2TypeLogin uint16 = 1 |
| 29 | V2TypeLoginResp uint16 = 2 |
| 30 | V2TypeNewProxy uint16 = 3 |
| 31 | V2TypeNewProxyResp uint16 = 4 |
| 32 | V2TypeCloseProxy uint16 = 5 |
| 33 | V2TypeNewWorkConn uint16 = 6 |
| 34 | V2TypeReqWorkConn uint16 = 7 |
| 35 | V2TypeStartWorkConn uint16 = 8 |
| 36 | V2TypeNewVisitorConn uint16 = 9 |
| 37 | V2TypeNewVisitorConnResp uint16 = 10 |
| 38 | V2TypePing uint16 = 11 |
| 39 | V2TypePong uint16 = 12 |
| 40 | V2TypeUDPPacket uint16 = 13 |
| 41 | V2TypeNatHoleVisitor uint16 = 14 |
| 42 | V2TypeNatHoleClient uint16 = 15 |
| 43 | V2TypeNatHoleResp uint16 = 16 |
| 44 | V2TypeNatHoleSid uint16 = 17 |
| 45 | V2TypeNatHoleReport uint16 = 18 |
| 46 | ) |
| 47 |
| 48 | var v2MsgTypeMap = map[uint16]any{ |
| 49 | V2TypeLogin: Login{}, |
| 50 | V2TypeLoginResp: LoginResp{}, |
| 51 | V2TypeNewProxy: NewProxy{}, |
| 52 | V2TypeNewProxyResp: NewProxyResp{}, |
| 53 | V2TypeCloseProxy: CloseProxy{}, |
| 54 | V2TypeNewWorkConn: NewWorkConn{}, |
| 55 | V2TypeReqWorkConn: ReqWorkConn{}, |
| 56 | V2TypeStartWorkConn: StartWorkConn{}, |
| 57 | V2TypeNewVisitorConn: NewVisitorConn{}, |
| 58 | V2TypeNewVisitorConnResp: NewVisitorConnResp{}, |
| 59 | V2TypePing: Ping{}, |
| 60 | V2TypePong: Pong{}, |
| 61 | V2TypeUDPPacket: UDPPacket{}, |
| 62 | V2TypeNatHoleVisitor: NatHoleVisitor{}, |
| 63 | V2TypeNatHoleClient: NatHoleClient{}, |
| 64 | V2TypeNatHoleResp: NatHoleResp{}, |
| 65 | V2TypeNatHoleSid: NatHoleSid{}, |
| 66 | V2TypeNatHoleReport: NatHoleReport{}, |
| 67 | } |
| 68 |
| 69 | var v2MsgReflectTypeMap, v2MsgTypeIDMap = buildV2MsgTypeMaps() |
| 70 |
| 71 | func buildV2MsgTypeMaps() (map[uint16]reflect.Type, map[reflect.Type]uint16) { |
| 72 | reflectTypeMap := make(map[uint16]reflect.Type, len(v2MsgTypeMap)) |
| 73 | typeIDMap := make(map[reflect.Type]uint16, len(v2MsgTypeMap)) |
| 74 | for typeID, m := range v2MsgTypeMap { |
| 75 | t := reflect.TypeOf(m) |
| 76 | reflectTypeMap[typeID] = t |
| 77 | typeIDMap[t] = typeID |
| 78 | } |
| 79 | return reflectTypeMap, typeIDMap |
| 80 | } |
| 81 |
| 82 | type V2ReadWriter struct { |
| 83 | conn *wire.Conn |
| 84 | } |
| 85 |
| 86 | func NewV2ReadWriter(rw io.ReadWriter) *V2ReadWriter { |
| 87 | return NewV2ReadWriterWithConn(wire.NewConn(rw)) |
| 88 | } |
| 89 |
| 90 | func NewV2ReadWriterWithConn(conn *wire.Conn) *V2ReadWriter { |
| 91 | return &V2ReadWriter{conn: conn} |
| 92 | } |
| 93 |
| 94 | func (rw *V2ReadWriter) WireConn() *wire.Conn { |
| 95 | return rw.conn |
| 96 | } |
| 97 |
| 98 | func (rw *V2ReadWriter) ReadMsg() (Message, error) { |
| 99 | f, err := rw.conn.ReadFrame() |
| 100 | if err != nil { |
| 101 | return nil, err |
| 102 | } |
| 103 | return DecodeV2MessageFrame(f) |
| 104 | } |
| 105 |
| 106 | func (rw *V2ReadWriter) ReadMsgInto(m Message) error { |
| 107 | f, err := rw.conn.ReadFrame() |
| 108 | if err != nil { |
| 109 | return err |
| 110 | } |
| 111 | return DecodeV2MessageFrameInto(f, m) |
| 112 | } |
| 113 |
| 114 | func (rw *V2ReadWriter) WriteMsg(m Message) error { |
| 115 | f, err := EncodeV2MessageFrame(m) |
| 116 | if err != nil { |
| 117 | return err |
| 118 | } |
| 119 | return rw.conn.WriteFrame(f) |
| 120 | } |
| 121 |
| 122 | func DecodeV2MessageFrame(f *wire.Frame) (Message, error) { |
| 123 | if f.Type != wire.FrameTypeMessage { |
| 124 | return nil, fmt.Errorf("unexpected frame type %d, want %d", f.Type, wire.FrameTypeMessage) |
| 125 | } |
| 126 | if len(f.Payload) < 2 { |
| 127 | return nil, fmt.Errorf("message frame payload too short") |
| 128 | } |
| 129 | typeID := binary.BigEndian.Uint16(f.Payload[:2]) |
| 130 | t, ok := v2MsgReflectTypeMap[typeID] |
| 131 | if !ok { |
| 132 | return nil, fmt.Errorf("unknown v2 message type %d", typeID) |
| 133 | } |
| 134 | m := reflect.New(t).Interface() |
| 135 | if err := json.Unmarshal(f.Payload[2:], m); err != nil { |
| 136 | return nil, err |
| 137 | } |
| 138 | return m, nil |
| 139 | } |
| 140 |
| 141 | func DecodeV2MessageFrameInto(f *wire.Frame, out Message) error { |
| 142 | if f.Type != wire.FrameTypeMessage { |
| 143 | return fmt.Errorf("unexpected frame type %d, want %d", f.Type, wire.FrameTypeMessage) |
| 144 | } |
| 145 | if len(f.Payload) < 2 { |
| 146 | return fmt.Errorf("message frame payload too short") |
| 147 | } |
| 148 |
| 149 | typeID := binary.BigEndian.Uint16(f.Payload[:2]) |
| 150 | outType := reflect.TypeOf(out) |
| 151 | if outType == nil || outType.Kind() != reflect.Pointer { |
| 152 | return fmt.Errorf("message target must be a pointer") |
| 153 | } |
| 154 | elemType := outType.Elem() |
| 155 | expectedTypeID, ok := v2MsgTypeIDMap[elemType] |
| 156 | if !ok { |
| 157 | return fmt.Errorf("unknown v2 message type %s", elemType.String()) |
| 158 | } |
| 159 | if typeID != expectedTypeID { |
| 160 | actualType, ok := v2MsgReflectTypeMap[typeID] |
| 161 | if !ok { |
| 162 | return fmt.Errorf("unknown v2 message type %d", typeID) |
| 163 | } |
| 164 | return fmt.Errorf("unexpected message type %s, want %s", actualType.String(), elemType.String()) |
| 165 | } |
| 166 | return json.Unmarshal(f.Payload[2:], out) |
| 167 | } |
| 168 |
| 169 | func EncodeV2MessageFrame(m Message) (*wire.Frame, error) { |
| 170 | t := reflect.TypeOf(m) |
| 171 | if t == nil { |
| 172 | return nil, fmt.Errorf("nil message") |
| 173 | } |
| 174 | if t.Kind() == reflect.Pointer { |
| 175 | t = t.Elem() |
| 176 | } |
| 177 | typeID, ok := v2MsgTypeIDMap[t] |
| 178 | if !ok { |
| 179 | return nil, fmt.Errorf("unknown v2 message type %s", t.String()) |
| 180 | } |
| 181 | content, err := json.Marshal(m) |
| 182 | if err != nil { |
| 183 | return nil, err |
| 184 | } |
| 185 | payload := make([]byte, 2+len(content)) |
| 186 | binary.BigEndian.PutUint16(payload[:2], typeID) |
| 187 | copy(payload[2:], content) |
| 188 | return &wire.Frame{ |
| 189 | Type: wire.FrameTypeMessage, |
| 190 | Payload: payload, |
| 191 | }, nil |
| 192 | } |