| // |
| 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 | "net" |
| 19 | "reflect" |
| 20 | ) |
| 21 |
| 22 | const ( |
| 23 | TypeLogin byte = 'o' |
| 24 | TypeLoginResp byte = '1' |
| 25 | TypeNewProxy byte = 'p' |
| 26 | TypeNewProxyResp byte = '2' |
| 27 | TypeCloseProxy byte = 'c' |
| 28 | TypeNewWorkConn byte = 'w' |
| 29 | TypeReqWorkConn byte = 'r' |
| 30 | TypeStartWorkConn byte = 's' |
| 31 | TypeNewVisitorConn byte = 'v' |
| 32 | TypeNewVisitorConnResp byte = '3' |
| 33 | TypePing byte = 'h' |
| 34 | TypePong byte = '4' |
| 35 | TypeUDPPacket byte = 'u' |
| 36 | TypeNatHoleVisitor byte = 'i' |
| 37 | TypeNatHoleClient byte = 'n' |
| 38 | TypeNatHoleResp byte = 'm' |
| 39 | TypeNatHoleSid byte = '5' |
| 40 | TypeNatHoleReport byte = '6' |
| 41 | ) |
| 42 |
| 43 | var msgTypeMap = map[byte]any{ |
| 44 | TypeLogin: Login{}, |
| 45 | TypeLoginResp: LoginResp{}, |
| 46 | TypeNewProxy: NewProxy{}, |
| 47 | TypeNewProxyResp: NewProxyResp{}, |
| 48 | TypeCloseProxy: CloseProxy{}, |
| 49 | TypeNewWorkConn: NewWorkConn{}, |
| 50 | TypeReqWorkConn: ReqWorkConn{}, |
| 51 | TypeStartWorkConn: StartWorkConn{}, |
| 52 | TypeNewVisitorConn: NewVisitorConn{}, |
| 53 | TypeNewVisitorConnResp: NewVisitorConnResp{}, |
| 54 | TypePing: Ping{}, |
| 55 | TypePong: Pong{}, |
| 56 | TypeUDPPacket: UDPPacket{}, |
| 57 | TypeNatHoleVisitor: NatHoleVisitor{}, |
| 58 | TypeNatHoleClient: NatHoleClient{}, |
| 59 | TypeNatHoleResp: NatHoleResp{}, |
| 60 | TypeNatHoleSid: NatHoleSid{}, |
| 61 | TypeNatHoleReport: NatHoleReport{}, |
| 62 | } |
| 63 |
| 64 | var TypeNameNatHoleResp = reflect.TypeFor[NatHoleResp]().Name() |
| 65 |
| 66 | type ClientSpec struct { |
| 67 | // Due to the support of VirtualClient, frps needs to know the client type in order to |
| 68 | // differentiate the processing logic. |
| 69 | // Optional values: ssh-tunnel |
| 70 | Type string `json:"type,omitempty"` |
| 71 | // If the value is true, the client will not require authentication. |
| 72 | AlwaysAuthPass bool `json:"always_auth_pass,omitempty"` |
| 73 | } |
| 74 |
| 75 | // When frpc start, client send this message to login to server. |
| 76 | type Login struct { |
| 77 | Version string `json:"version,omitempty"` |
| 78 | Hostname string `json:"hostname,omitempty"` |
| 79 | Os string `json:"os,omitempty"` |
| 80 | Arch string `json:"arch,omitempty"` |
| 81 | User string `json:"user,omitempty"` |
| 82 | PrivilegeKey string `json:"privilege_key,omitempty"` |
| 83 | Timestamp int64 `json:"timestamp,omitempty"` |
| 84 | RunID string `json:"run_id,omitempty"` |
| 85 | ClientID string `json:"client_id,omitempty"` |
| 86 | Metas map[string]string `json:"metas,omitempty"` |
| 87 |
| 88 | // Currently only effective for VirtualClient. |
| 89 | ClientSpec ClientSpec `json:"client_spec,omitempty"` |
| 90 |
| 91 | // Some global configures. |
| 92 | PoolCount int `json:"pool_count,omitempty"` |
| 93 | } |
| 94 |
| 95 | type LoginResp struct { |
| 96 | Version string `json:"version,omitempty"` |
| 97 | RunID string `json:"run_id,omitempty"` |
| 98 | Error string `json:"error,omitempty"` |
| 99 | } |
| 100 |
| 101 | // When frpc login success, send this message to frps for running a new proxy. |
| 102 | type NewProxy struct { |
| 103 | ProxyName string `json:"proxy_name,omitempty"` |
| 104 | ProxyType string `json:"proxy_type,omitempty"` |
| 105 | UseEncryption bool `json:"use_encryption,omitempty"` |
| 106 | UseCompression bool `json:"use_compression,omitempty"` |
| 107 | BandwidthLimit string `json:"bandwidth_limit,omitempty"` |
| 108 | BandwidthLimitMode string `json:"bandwidth_limit_mode,omitempty"` |
| 109 | Group string `json:"group,omitempty"` |
| 110 | GroupKey string `json:"group_key,omitempty"` |
| 111 | Metas map[string]string `json:"metas,omitempty"` |
| 112 | Annotations map[string]string `json:"annotations,omitempty"` |
| 113 |
| 114 | // tcp and udp only |
| 115 | RemotePort int `json:"remote_port,omitempty"` |
| 116 |
| 117 | // http and https only |
| 118 | CustomDomains []string `json:"custom_domains,omitempty"` |
| 119 | SubDomain string `json:"subdomain,omitempty"` |
| 120 | Locations []string `json:"locations,omitempty"` |
| 121 | HTTPUser string `json:"http_user,omitempty"` |
| 122 | HTTPPwd string `json:"http_pwd,omitempty"` |
| 123 | HostHeaderRewrite string `json:"host_header_rewrite,omitempty"` |
| 124 | Headers map[string]string `json:"headers,omitempty"` |
| 125 | ResponseHeaders map[string]string `json:"response_headers,omitempty"` |
| 126 | RouteByHTTPUser string `json:"route_by_http_user,omitempty"` |
| 127 |
| 128 | // stcp, sudp, xtcp |
| 129 | Sk string `json:"sk,omitempty"` |
| 130 | AllowUsers []string `json:"allow_users,omitempty"` |
| 131 |
| 132 | // tcpmux |
| 133 | Multiplexer string `json:"multiplexer,omitempty"` |
| 134 | } |
| 135 |
| 136 | type NewProxyResp struct { |
| 137 | ProxyName string `json:"proxy_name,omitempty"` |
| 138 | RemoteAddr string `json:"remote_addr,omitempty"` |
| 139 | Error string `json:"error,omitempty"` |
| 140 | } |
| 141 |
| 142 | type CloseProxy struct { |
| 143 | ProxyName string `json:"proxy_name,omitempty"` |
| 144 | } |
| 145 |
| 146 | type NewWorkConn struct { |
| 147 | RunID string `json:"run_id,omitempty"` |
| 148 | PrivilegeKey string `json:"privilege_key,omitempty"` |
| 149 | Timestamp int64 `json:"timestamp,omitempty"` |
| 150 | } |
| 151 |
| 152 | type ReqWorkConn struct{} |
| 153 |
| 154 | type StartWorkConn struct { |
| 155 | ProxyName string `json:"proxy_name,omitempty"` |
| 156 | SrcAddr string `json:"src_addr,omitempty"` |
| 157 | DstAddr string `json:"dst_addr,omitempty"` |
| 158 | SrcPort uint16 `json:"src_port,omitempty"` |
| 159 | DstPort uint16 `json:"dst_port,omitempty"` |
| 160 | Error string `json:"error,omitempty"` |
| 161 | } |
| 162 |
| 163 | type NewVisitorConn struct { |
| 164 | RunID string `json:"run_id,omitempty"` |
| 165 | ProxyName string `json:"proxy_name,omitempty"` |
| 166 | SignKey string `json:"sign_key,omitempty"` |
| 167 | Timestamp int64 `json:"timestamp,omitempty"` |
| 168 | UseEncryption bool `json:"use_encryption,omitempty"` |
| 169 | UseCompression bool `json:"use_compression,omitempty"` |
| 170 | } |
| 171 |
| 172 | type NewVisitorConnResp struct { |
| 173 | ProxyName string `json:"proxy_name,omitempty"` |
| 174 | Error string `json:"error,omitempty"` |
| 175 | } |
| 176 |
| 177 | type Ping struct { |
| 178 | PrivilegeKey string `json:"privilege_key,omitempty"` |
| 179 | Timestamp int64 `json:"timestamp,omitempty"` |
| 180 | } |
| 181 |
| 182 | type Pong struct { |
| 183 | Error string `json:"error,omitempty"` |
| 184 | } |
| 185 |
| 186 | type UDPPacket struct { |
| 187 | Content []byte `json:"c,omitempty"` |
| 188 | LocalAddr *net.UDPAddr `json:"l,omitempty"` |
| 189 | RemoteAddr *net.UDPAddr `json:"r,omitempty"` |
| 190 | } |
| 191 |
| 192 | type NatHoleVisitor struct { |
| 193 | TransactionID string `json:"transaction_id,omitempty"` |
| 194 | ProxyName string `json:"proxy_name,omitempty"` |
| 195 | PreCheck bool `json:"pre_check,omitempty"` |
| 196 | Protocol string `json:"protocol,omitempty"` |
| 197 | SignKey string `json:"sign_key,omitempty"` |
| 198 | Timestamp int64 `json:"timestamp,omitempty"` |
| 199 | MappedAddrs []string `json:"mapped_addrs,omitempty"` |
| 200 | AssistedAddrs []string `json:"assisted_addrs,omitempty"` |
| 201 | } |
| 202 |
| 203 | type NatHoleClient struct { |
| 204 | TransactionID string `json:"transaction_id,omitempty"` |
| 205 | ProxyName string `json:"proxy_name,omitempty"` |
| 206 | Sid string `json:"sid,omitempty"` |
| 207 | MappedAddrs []string `json:"mapped_addrs,omitempty"` |
| 208 | AssistedAddrs []string `json:"assisted_addrs,omitempty"` |
| 209 | } |
| 210 |
| 211 | type PortsRange struct { |
| 212 | From int `json:"from,omitempty"` |
| 213 | To int `json:"to,omitempty"` |
| 214 | } |
| 215 |
| 216 | type NatHoleDetectBehavior struct { |
| 217 | Role string `json:"role,omitempty"` // sender or receiver |
| 218 | Mode int `json:"mode,omitempty"` // 0, 1, 2... |
| 219 | TTL int `json:"ttl,omitempty"` |
| 220 | SendDelayMs int `json:"send_delay_ms,omitempty"` |
| 221 | ReadTimeoutMs int `json:"read_timeout,omitempty"` |
| 222 | CandidatePorts []PortsRange `json:"candidate_ports,omitempty"` |
| 223 | SendRandomPorts int `json:"send_random_ports,omitempty"` |
| 224 | ListenRandomPorts int `json:"listen_random_ports,omitempty"` |
| 225 | } |
| 226 |
| 227 | type NatHoleResp struct { |
| 228 | TransactionID string `json:"transaction_id,omitempty"` |
| 229 | Sid string `json:"sid,omitempty"` |
| 230 | Protocol string `json:"protocol,omitempty"` |
| 231 | CandidateAddrs []string `json:"candidate_addrs,omitempty"` |
| 232 | AssistedAddrs []string `json:"assisted_addrs,omitempty"` |
| 233 | DetectBehavior NatHoleDetectBehavior `json:"detect_behavior,omitempty"` |
| 234 | Error string `json:"error,omitempty"` |
| 235 | } |
| 236 |
| 237 | type NatHoleSid struct { |
| 238 | TransactionID string `json:"transaction_id,omitempty"` |
| 239 | Sid string `json:"sid,omitempty"` |
| 240 | Response bool `json:"response,omitempty"` |
| 241 | Nonce string `json:"nonce,omitempty"` |
| 242 | } |
| 243 |
| 244 | type NatHoleReport struct { |
| 245 | Sid string `json:"sid,omitempty"` |
| 246 | Success bool `json:"success,omitempty"` |
| 247 | } |