| // |
| 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 wire |
| 16 |
| 17 | import ( |
| 18 | "encoding/binary" |
| 19 | "encoding/json" |
| 20 | "fmt" |
| 21 | "io" |
| 22 | "net" |
| 23 | "slices" |
| 24 |
| 25 | libnet "github.com/fatedier/golib/net" |
| 26 | ) |
| 27 |
| 28 | const ( |
| 29 | ProtocolV1 = "v1" |
| 30 | ProtocolV2 = "v2" |
| 31 |
| 32 | WireVersionV2 = 2 |
| 33 |
| 34 | FrameTypeClientHello uint16 = 1 |
| 35 | FrameTypeServerHello uint16 = 2 |
| 36 | FrameTypeMessage uint16 = 16 |
| 37 |
| 38 | MessageCodecJSON = "json" |
| 39 | DefaultMaxFramePayloadSize = 64 * 1024 |
| 40 |
| 41 | MagicV2 = "FRP\x00\x02\r\n" |
| 42 | ) |
| 43 |
| 44 | type Frame struct { |
| 45 | Type uint16 |
| 46 | Flags uint16 |
| 47 | Payload []byte |
| 48 | } |
| 49 |
| 50 | type Conn struct { |
| 51 | rw io.ReadWriter |
| 52 | maxFramePayloadSize uint32 |
| 53 | } |
| 54 |
| 55 | func NewConn(rw io.ReadWriter) *Conn { |
| 56 | return &Conn{ |
| 57 | rw: rw, |
| 58 | maxFramePayloadSize: DefaultMaxFramePayloadSize, |
| 59 | } |
| 60 | } |
| 61 |
| 62 | func (c *Conn) ReadFrame() (*Frame, error) { |
| 63 | header := make([]byte, 8) |
| 64 | if _, err := io.ReadFull(c.rw, header); err != nil { |
| 65 | return nil, err |
| 66 | } |
| 67 |
| 68 | frameType := binary.BigEndian.Uint16(header[0:2]) |
| 69 | flags := binary.BigEndian.Uint16(header[2:4]) |
| 70 | length := binary.BigEndian.Uint32(header[4:8]) |
| 71 | if flags != 0 { |
| 72 | return nil, fmt.Errorf("unsupported frame flags: %d", flags) |
| 73 | } |
| 74 | if length > c.maxFramePayloadSize { |
| 75 | return nil, fmt.Errorf("frame payload length %d exceeds limit %d", length, c.maxFramePayloadSize) |
| 76 | } |
| 77 |
| 78 | payload := make([]byte, length) |
| 79 | if _, err := io.ReadFull(c.rw, payload); err != nil { |
| 80 | return nil, err |
| 81 | } |
| 82 | return &Frame{ |
| 83 | Type: frameType, |
| 84 | Flags: flags, |
| 85 | Payload: payload, |
| 86 | }, nil |
| 87 | } |
| 88 |
| 89 | func (c *Conn) WriteFrame(f *Frame) error { |
| 90 | if f.Flags != 0 { |
| 91 | return fmt.Errorf("unsupported frame flags: %d", f.Flags) |
| 92 | } |
| 93 | if len(f.Payload) > int(c.maxFramePayloadSize) { |
| 94 | return fmt.Errorf("frame payload length %d exceeds limit %d", len(f.Payload), c.maxFramePayloadSize) |
| 95 | } |
| 96 |
| 97 | header := make([]byte, 8) |
| 98 | binary.BigEndian.PutUint16(header[0:2], f.Type) |
| 99 | binary.BigEndian.PutUint16(header[2:4], f.Flags) |
| 100 | binary.BigEndian.PutUint32(header[4:8], uint32(len(f.Payload))) |
| 101 | if _, err := c.rw.Write(header); err != nil { |
| 102 | return err |
| 103 | } |
| 104 | _, err := c.rw.Write(f.Payload) |
| 105 | return err |
| 106 | } |
| 107 |
| 108 | func (c *Conn) ReadJSONFrame(frameType uint16, out any) error { |
| 109 | f, err := c.ReadFrame() |
| 110 | if err != nil { |
| 111 | return err |
| 112 | } |
| 113 | if f.Type != frameType { |
| 114 | return fmt.Errorf("unexpected frame type %d, want %d", f.Type, frameType) |
| 115 | } |
| 116 | return c.UnmarshalFrame(f, out) |
| 117 | } |
| 118 |
| 119 | func (c *Conn) UnmarshalFrame(f *Frame, out any) error { |
| 120 | return json.Unmarshal(f.Payload, out) |
| 121 | } |
| 122 |
| 123 | func NewJSONFrame(frameType uint16, in any) (*Frame, error) { |
| 124 | payload, err := json.Marshal(in) |
| 125 | if err != nil { |
| 126 | return nil, err |
| 127 | } |
| 128 | return &Frame{ |
| 129 | Type: frameType, |
| 130 | Payload: payload, |
| 131 | }, nil |
| 132 | } |
| 133 |
| 134 | func (c *Conn) WriteJSONFrame(frameType uint16, in any) error { |
| 135 | f, err := NewJSONFrame(frameType, in) |
| 136 | if err != nil { |
| 137 | return err |
| 138 | } |
| 139 | return c.WriteFrame(f) |
| 140 | } |
| 141 |
| 142 | func WriteMagic(w io.Writer) error { |
| 143 | _, err := io.WriteString(w, MagicV2) |
| 144 | return err |
| 145 | } |
| 146 |
| 147 | func WriteMagicIfV2(w io.Writer, wireProtocol string) error { |
| 148 | if wireProtocol != ProtocolV2 { |
| 149 | return nil |
| 150 | } |
| 151 | return WriteMagic(w) |
| 152 | } |
| 153 |
| 154 | func CheckMagic(conn net.Conn) (out net.Conn, isV2 bool, err error) { |
| 155 | sharedConn, r := libnet.NewSharedConnSize(conn, len(MagicV2)) |
| 156 | buf := make([]byte, len(MagicV2)) |
| 157 | if _, err = io.ReadFull(r, buf); err != nil { |
| 158 | return nil, false, err |
| 159 | } |
| 160 | for i := range MagicV2 { |
| 161 | if buf[i] != MagicV2[i] { |
| 162 | return sharedConn, false, nil |
| 163 | } |
| 164 | } |
| 165 | return conn, true, nil |
| 166 | } |
| 167 |
| 168 | type BootstrapInfo struct { |
| 169 | Transport string `json:"transport,omitempty"` |
| 170 | TLS bool `json:"tls,omitempty"` |
| 171 | TCPMux bool `json:"tcpMux,omitempty"` |
| 172 | } |
| 173 |
| 174 | type ClientHello struct { |
| 175 | Bootstrap BootstrapInfo `json:"bootstrap,omitempty"` |
| 176 | Capabilities ClientCapabilities `json:"capabilities,omitempty"` |
| 177 | } |
| 178 |
| 179 | type ClientCapabilities struct { |
| 180 | Message MessageCapabilities `json:"message,omitempty"` |
| 181 | Crypto CryptoCapabilities `json:"crypto,omitempty"` |
| 182 | } |
| 183 |
| 184 | type MessageCapabilities struct { |
| 185 | Codecs []string `json:"codecs,omitempty"` |
| 186 | } |
| 187 |
| 188 | type CryptoCapabilities struct { |
| 189 | Algorithms []string `json:"algorithms,omitempty"` |
| 190 | ClientRandom []byte `json:"clientRandom,omitempty"` |
| 191 | } |
| 192 |
| 193 | type ServerHello struct { |
| 194 | Selected ServerSelection `json:"selected,omitempty"` |
| 195 | Error string `json:"error,omitempty"` |
| 196 | } |
| 197 |
| 198 | type ServerSelection struct { |
| 199 | Message MessageSelection `json:"message,omitempty"` |
| 200 | Crypto CryptoSelection `json:"crypto,omitempty"` |
| 201 | } |
| 202 |
| 203 | type MessageSelection struct { |
| 204 | Codec string `json:"codec,omitempty"` |
| 205 | } |
| 206 |
| 207 | type CryptoSelection struct { |
| 208 | Algorithm string `json:"algorithm,omitempty"` |
| 209 | ServerRandom []byte `json:"serverRandom,omitempty"` |
| 210 | } |
| 211 |
| 212 | func clientHelloWithCryptoRandom(bootstrap BootstrapInfo, clientRandom []byte) ClientHello { |
| 213 | return ClientHello{ |
| 214 | Bootstrap: bootstrap, |
| 215 | Capabilities: ClientCapabilities{ |
| 216 | Message: MessageCapabilities{ |
| 217 | Codecs: []string{MessageCodecJSON}, |
| 218 | }, |
| 219 | Crypto: CryptoCapabilities{ |
| 220 | Algorithms: PreferredAEADAlgorithms(), |
| 221 | ClientRandom: clientRandom, |
| 222 | }, |
| 223 | }, |
| 224 | } |
| 225 | } |
| 226 |
| 227 | func DefaultServerHello() ServerHello { |
| 228 | return ServerHello{ |
| 229 | Selected: ServerSelection{ |
| 230 | Message: MessageSelection{ |
| 231 | Codec: MessageCodecJSON, |
| 232 | }, |
| 233 | }, |
| 234 | } |
| 235 | } |
| 236 |
| 237 | func Supports(list []string, value string) bool { |
| 238 | return slices.Contains(list, value) |
| 239 | } |
| 240 |
| 241 | func ValidateClientHello(h ClientHello) error { |
| 242 | if !Supports(h.Capabilities.Message.Codecs, MessageCodecJSON) { |
| 243 | return fmt.Errorf("unsupported message codec") |
| 244 | } |
| 245 | return ValidateCryptoCapabilities(h.Capabilities.Crypto) |
| 246 | } |