| 1 | ### Server Plugin |
| 2 | |
| 3 | frp server plugin is aimed to extend frp's ability without modifying the Golang code. |
| 4 | |
| 5 | An external server should run in a different process receiving RPC calls from frps. |
| 6 | Before frps is doing some operations, it will send RPC requests to notify the external RPC server and act according to its response. |
| 7 | |
| 8 |
| ### RPC request |
| 9 |
| 10 | RPC requests are based on JSON over HTTP. |
| 11 |
| 12 | When a server plugin accepts an operation request, it can respond with three different responses: |
| 13 |
| 14 | * Reject operation and return a reason. |
| 15 | * Allow operation and keep original content. |
| 16 | * Allow operation and return modified content. |
| 17 |
| 18 | ### Interface |
| 19 |
| 20 | HTTP path can be configured for each manage plugin in frps. We'll assume for this example that it's `/handler`. |
| 21 |
| 22 | A request to the RPC server will look like: |
| 23 |
| 24 | ``` |
| 25 | POST /handler?version=0.1.0&op=Login |
| 26 | { |
| 27 | "version": "0.1.0", |
| 28 | "op": "Login", |
| 29 | "content": { |
| 30 | ... // Operation info |
| 31 | } |
| 32 | } |
| 33 |
| 34 | Request Header: |
| 35 | X-Frp-Reqid: for tracing |
| 36 | ``` |
| 37 |
| 38 | The response can look like any of the following: |
| 39 |
| 40 | * Non-200 HTTP response status code (this will automatically tell frps that the request should fail) |
| 41 |
| 42 | * Reject operation: |
| 43 |
| 44 | ``` |
| 45 | { |
| 46 | "reject": true, |
| 47 | "reject_reason": "invalid user" |
| 48 | } |
| 49 | ``` |
| 50 |
| 51 | * Allow operation and keep original content: |
| 52 |
| 53 | ``` |
| 54 | { |
| 55 | "reject": false, |
| 56 | "unchange": true |
| 57 | } |
| 58 | ``` |
| 59 |
| 60 | * Allow operation and modify content |
| 61 |
| 62 | ``` |
| 63 | { |
| 64 | "unchange": "false", |
| 65 | "content": { |
| 66 | ... // Replaced content |
| 67 | } |
| 68 | } |
| 69 | ``` |
| 70 |
| 71 | ### Operation |
| 72 |
| 73 | Currently `Login`, `NewProxy`, `CloseProxy`, `Ping`, `NewWorkConn` and `NewUserConn` operations are supported. |
| 74 |
| 75 | #### Login |
| 76 |
| 77 | Client login operation |
| 78 |
| 79 | ``` |
| 80 | { |
| 81 | "content": { |
| 82 | "version": <string>, |
| 83 | "hostname": <string>, |
| 84 | "os": <string>, |
| 85 | "arch": <string>, |
| 86 | "user": <string>, |
| 87 | "timestamp": <int64>, |
| 88 | "privilege_key": <string>, |
| 89 | "run_id": <string>, |
| 90 | "pool_count": <int>, |
| 91 | "metas": map<string>string, |
| 92 | "client_address": <string> |
| 93 | } |
| 94 | } |
| 95 | ``` |
| 96 |
| 97 | #### NewProxy |
| 98 |
| 99 | Create new proxy |
| 100 |
| 101 | ``` |
| 102 | { |
| 103 | "content": { |
| 104 | "user": { |
| 105 | "user": <string>, |
| 106 | "metas": map<string>string |
| 107 | "run_id": <string> |
| 108 | }, |
| 109 | "proxy_name": <string>, |
| 110 | "proxy_type": <string>, |
| 111 | "use_encryption": <bool>, |
| 112 | "use_compression": <bool>, |
| 113 | "bandwidth_limit": <string>, |
| 114 | "bandwidth_limit_mode": <string>, |
| 115 | "group": <string>, |
| 116 | "group_key": <string>, |
| 117 |
| 118 | // tcp and udp only |
| 119 | "remote_port": <int>, |
| 120 |
| 121 | // http and https only |
| 122 | "custom_domains": []<string>, |
| 123 | "subdomain": <string>, |
| 124 | "locations": []<string>, |
| 125 | "http_user": <string>, |
| 126 | "http_pwd": <string>, |
| 127 | "host_header_rewrite": <string>, |
| 128 | "headers": map<string>string, |
| 129 |
| 130 | // stcp only |
| 131 | "sk": <string>, |
| 132 |
| 133 | // tcpmux only |
| 134 | "multiplexer": <string> |
| 135 |
| 136 | "metas": map<string>string |
| 137 | } |
| 138 | } |
| 139 | ``` |
| 140 |
| 141 | #### CloseProxy |
| 142 |
| 143 | A previously created proxy is closed. |
| 144 |
| 145 | Please note that one request will be sent for every proxy that is closed, do **NOT** use this |
| 146 | if you have too many proxies bound to a single client, as this may exhaust the server's resources. |
| 147 |
| 148 | ``` |
| 149 | { |
| 150 | "content": { |
| 151 | "user": { |
| 152 | "user": <string>, |
| 153 | "metas": map<string>string |
| 154 | "run_id": <string> |
| 155 | }, |
| 156 | "proxy_name": <string> |
| 157 | } |
| 158 | } |
| 159 | ``` |
| 160 |
| 161 | #### Ping |
| 162 |
| 163 | Heartbeat from frpc |
| 164 |
| 165 | ``` |
| 166 | { |
| 167 | "content": { |
| 168 | "user": { |
| 169 | "user": <string>, |
| 170 | "metas": map<string>string |
| 171 | "run_id": <string> |
| 172 | }, |
| 173 | "timestamp": <int64>, |
| 174 | "privilege_key": <string> |
| 175 | } |
| 176 | } |
| 177 | ``` |
| 178 |
| 179 | #### NewWorkConn |
| 180 |
| 181 | New work connection received from frpc (RPC sent after `run_id` is matched with an existing frp connection) |
| 182 |
| 183 | ``` |
| 184 | { |
| 185 | "content": { |
| 186 | "user": { |
| 187 | "user": <string>, |
| 188 | "metas": map<string>string |
| 189 | "run_id": <string> |
| 190 | }, |
| 191 | "run_id": <string> |
| 192 | "timestamp": <int64>, |
| 193 | "privilege_key": <string> |
| 194 | } |
| 195 | } |
| 196 | ``` |
| 197 |
| 198 | #### NewUserConn |
| 199 |
| 200 | New user connection received from proxy (support `tcp`, `stcp`, `https` and `tcpmux`) . |
| 201 |
| 202 | ``` |
| 203 | { |
| 204 | "content": { |
| 205 | "user": { |
| 206 | "user": <string>, |
| 207 | "metas": map<string>string |
| 208 | "run_id": <string> |
| 209 | }, |
| 210 | "proxy_name": <string>, |
| 211 | "proxy_type": <string>, |
| 212 | "remote_addr": <string> |
| 213 | } |
| 214 | } |
| 215 | ``` |
| 216 |
| 217 | ### Server Plugin Configuration |
| 218 |
| 219 | ```toml |
| 220 | # frps.toml |
| 221 | bindPort = 7000 |
| 222 |
| 223 | [[httpPlugins]] |
| 224 | name = "user-manager" |
| 225 | addr = "127.0.0.1:9000" |
| 226 | path = "/handler" |
| 227 | ops = ["Login"] |
| 228 |
| 229 | [[httpPlugins]] |
| 230 | name = "port-manager" |
| 231 | addr = "127.0.0.1:9001" |
| 232 | path = "/handler" |
| 233 | ops = ["NewProxy"] |
| 234 | ``` |
| 235 |
| 236 | - addr: the address where the external RPC service listens. Defaults to http. For https, specify the schema: `addr = "https://127.0.0.1:9001"`. |
| 237 | - path: http request url path for the POST request. |
| 238 | - ops: operations plugin needs to handle (e.g. "Login", "NewProxy", ...). |
| 239 | - tlsVerify: When the schema is https, we verify by default. Set this value to false if you want to skip verification. |
| 240 |
| 241 | ### Metadata |
| 242 |
| 243 | Metadata will be sent to the server plugin in each RPC request. |
| 244 |
| 245 | There are 2 types of metadata entries - global one and the other under each proxy configuration. |
| 246 | Global metadata entries will be sent in `Login` under the key `metas`, and in any other RPC request under `user.metas`. |
| 247 | Metadata entries under each proxy configuration will be sent in `NewProxy` op only, under `metas`. |
| 248 |
| 249 | This is an example of metadata entries: |
| 250 |
| 251 | ```toml |
| 252 | # frpc.toml |
| 253 | serverAddr = "127.0.0.1" |
| 254 | serverPort = 7000 |
| 255 | user = "fake" |
| 256 | metadatas.token = "fake" |
| 257 | metadatas.version = "1.0.0" |
| 258 |
| 259 | [[proxies]] |
| 260 | name = "ssh" |
| 261 | type = "tcp" |
| 262 | localPort = 22 |
| 263 | remotePort = 6000 |
| 264 | metadatas.id = "123" |
| 265 | ``` |