| // |
| 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 http |
| 16 |
| 17 | import ( |
| 18 | "cmp" |
| 19 | "fmt" |
| 20 | "maps" |
| 21 | "math" |
| 22 | "net/http" |
| 23 | "net/url" |
| 24 | "slices" |
| 25 | "strconv" |
| 26 | "strings" |
| 27 | "time" |
| 28 |
| 29 | v1 "github.com/fatedier/frp/pkg/config/v1" |
| 30 | "github.com/fatedier/frp/pkg/metrics/mem" |
| 31 | httppkg "github.com/fatedier/frp/pkg/util/http" |
| 32 | "github.com/fatedier/frp/server/http/model" |
| 33 | "github.com/fatedier/frp/server/registry" |
| 34 | ) |
| 35 |
| 36 | const ( |
| 37 | defaultV2Page = 1 |
| 38 | defaultV2PageSize = 50 |
| 39 | maxV2PageSize = 200 |
| 40 |
| 41 | v2SystemPruneTypeOfflineProxies = "offline_proxies" |
| 42 | v2ProxyTrafficDefaultDays = 7 |
| 43 | v2ProxyTrafficUnit = "bytes" |
| 44 | v2ProxyTrafficGranularity = "day" |
| 45 | ) |
| 46 |
| 47 | var apiV2ProxyTypes = []string{ |
| 48 | string(v1.ProxyTypeTCP), |
| 49 | string(v1.ProxyTypeUDP), |
| 50 | string(v1.ProxyTypeHTTP), |
| 51 | string(v1.ProxyTypeHTTPS), |
| 52 | string(v1.ProxyTypeTCPMUX), |
| 53 | string(v1.ProxyTypeSTCP), |
| 54 | string(v1.ProxyTypeXTCP), |
| 55 | string(v1.ProxyTypeSUDP), |
| 56 | } |
| 57 |
| 58 | // /api/v2/system/info |
| 59 | func (c *Controller) APIV2SystemInfo(ctx *httppkg.Context) (any, error) { |
| 60 | info := c.buildServerInfoResp() |
| 61 | proxyTypeCounts := info.ProxyTypeCounts |
| 62 | if proxyTypeCounts == nil { |
| 63 | proxyTypeCounts = map[string]int64{} |
| 64 | } |
| 65 |
| 66 | return model.V2SystemInfoResp{ |
| 67 | Version: info.Version, |
| 68 | Config: model.V2SystemInfoConfigResp{ |
| 69 | BindPort: info.BindPort, |
| 70 | VhostHTTPPort: info.VhostHTTPPort, |
| 71 | VhostHTTPSPort: info.VhostHTTPSPort, |
| 72 | TCPMuxHTTPConnectPort: info.TCPMuxHTTPConnectPort, |
| 73 | KCPBindPort: info.KCPBindPort, |
| 74 | QUICBindPort: info.QUICBindPort, |
| 75 | SubdomainHost: info.SubdomainHost, |
| 76 | MaxPoolCount: info.MaxPoolCount, |
| 77 | MaxPortsPerClient: info.MaxPortsPerClient, |
| 78 | HeartbeatTimeout: info.HeartBeatTimeout, |
| 79 | AllowPortsStr: info.AllowPortsStr, |
| 80 | TLSForce: info.TLSForce, |
| 81 | }, |
| 82 | Status: model.V2SystemInfoStatusResp{ |
| 83 | TotalTrafficIn: info.TotalTrafficIn, |
| 84 | TotalTrafficOut: info.TotalTrafficOut, |
| 85 | CurConns: info.CurConns, |
| 86 | ClientCounts: info.ClientCounts, |
| 87 | ProxyTypeCounts: proxyTypeCounts, |
| 88 | }, |
| 89 | }, nil |
| 90 | } |
| 91 |
| 92 | // /api/v2/system/prune |
| 93 | func (c *Controller) APIV2SystemPrune(ctx *httppkg.Context) (any, error) { |
| 94 | pruneType, err := parseV2SystemPruneType(ctx.Query("type")) |
| 95 | if err != nil { |
| 96 | return nil, err |
| 97 | } |
| 98 |
| 99 | cleared, total := mem.StatsCollector.PruneOfflineProxies() |
| 100 | return model.V2SystemPruneResp{ |
| 101 | Type: pruneType, |
| 102 | Cleared: cleared, |
| 103 | Total: total, |
| 104 | }, nil |
| 105 | } |
| 106 |
| 107 | // /api/v2/users |
| 108 | func (c *Controller) APIV2UserList(ctx *httppkg.Context) (any, error) { |
| 109 | page, pageSize, err := parseV2PageParams(ctx) |
| 110 | if err != nil { |
| 111 | return nil, err |
| 112 | } |
| 113 | if c.clientRegistry == nil { |
| 114 | return nil, fmt.Errorf("client registry unavailable") |
| 115 | } |
| 116 |
| 117 | userStats := make(map[string]*model.V2UserResp) |
| 118 | for _, info := range c.clientRegistry.List() { |
| 119 | item := getOrCreateV2User(userStats, info.User) |
| 120 | item.ClientCount++ |
| 121 | } |
| 122 | for _, proxyInfo := range c.listV2ProxyStats("") { |
| 123 | item := getOrCreateV2User(userStats, proxyInfo.User) |
| 124 | item.ProxyCount++ |
| 125 | } |
| 126 |
| 127 | q := strings.ToLower(ctx.Query("q")) |
| 128 | items := make([]model.V2UserResp, 0, len(userStats)) |
| 129 | for _, item := range userStats { |
| 130 | if q != "" && !strings.Contains(strings.ToLower(item.User), q) { |
| 131 | continue |
| 132 | } |
| 133 | items = append(items, *item) |
| 134 | } |
| 135 | slices.SortFunc(items, func(a, b model.V2UserResp) int { |
| 136 | return cmp.Compare(a.User, b.User) |
| 137 | }) |
| 138 |
| 139 | return buildV2PageResp(items, page, pageSize), nil |
| 140 | } |
| 141 |
| 142 | // /api/v2/clients |
| 143 | func (c *Controller) APIV2ClientList(ctx *httppkg.Context) (any, error) { |
| 144 | page, pageSize, err := parseV2PageParams(ctx) |
| 145 | if err != nil { |
| 146 | return nil, err |
| 147 | } |
| 148 | if c.clientRegistry == nil { |
| 149 | return nil, fmt.Errorf("client registry unavailable") |
| 150 | } |
| 151 | statusFilter, err := parseV2StatusFilter(ctx.Query("status")) |
| 152 | if err != nil { |
| 153 | return nil, err |
| 154 | } |
| 155 |
| 156 | userFilter, filterByUser := queryValue(ctx, "user") |
| 157 | clientIDFilter := ctx.Query("clientID") |
| 158 | runIDFilter := ctx.Query("runID") |
| 159 | q := strings.ToLower(ctx.Query("q")) |
| 160 |
| 161 | records := c.clientRegistry.List() |
| 162 | items := make([]model.ClientInfoResp, 0, len(records)) |
| 163 | for _, info := range records { |
| 164 | if filterByUser && info.User != userFilter { |
| 165 | continue |
| 166 | } |
| 167 | if clientIDFilter != "" && info.ClientID() != clientIDFilter { |
| 168 | continue |
| 169 | } |
| 170 | if runIDFilter != "" && info.RunID != runIDFilter { |
| 171 | continue |
| 172 | } |
| 173 | if !matchV2StatusFilter(info.Online, statusFilter) { |
| 174 | continue |
| 175 | } |
| 176 | resp := buildClientInfoResp(info) |
| 177 | if q != "" && !matchV2ClientQuery(resp, q) { |
| 178 | continue |
| 179 | } |
| 180 | items = append(items, resp) |
| 181 | } |
| 182 |
| 183 | slices.SortFunc(items, func(a, b model.ClientInfoResp) int { |
| 184 | if v := cmp.Compare(a.User, b.User); v != 0 { |
| 185 | return v |
| 186 | } |
| 187 | if v := cmp.Compare(a.ClientID, b.ClientID); v != 0 { |
| 188 | return v |
| 189 | } |
| 190 | return cmp.Compare(a.Key, b.Key) |
| 191 | }) |
| 192 |
| 193 | return buildV2PageResp(items, page, pageSize), nil |
| 194 | } |
| 195 |
| 196 | // /api/v2/clients/{key} |
| 197 | func (c *Controller) APIV2ClientDetail(ctx *httppkg.Context) (any, error) { |
| 198 | key, err := decodeV2PathParam(ctx, "key", "client key") |
| 199 | if err != nil { |
| 200 | return nil, err |
| 201 | } |
| 202 |
| 203 | if c.clientRegistry == nil { |
| 204 | return nil, fmt.Errorf("client registry unavailable") |
| 205 | } |
| 206 |
| 207 | info, ok := c.clientRegistry.GetByKey(key) |
| 208 | if !ok { |
| 209 | return nil, httppkg.NewError(http.StatusNotFound, fmt.Sprintf("client %s not found", key)) |
| 210 | } |
| 211 |
| 212 | resp := buildClientInfoResp(info) |
| 213 | status := c.buildV2ClientStatus(info) |
| 214 | return model.V2ClientDetailResp{ |
| 215 | ClientInfoResp: resp, |
| 216 | Status: status, |
| 217 | }, nil |
| 218 | } |
| 219 |
| 220 | // /api/v2/proxies |
| 221 | func (c *Controller) APIV2ProxyList(ctx *httppkg.Context) (any, error) { |
| 222 | page, pageSize, err := parseV2PageParams(ctx) |
| 223 | if err != nil { |
| 224 | return nil, err |
| 225 | } |
| 226 | statusFilter, err := parseV2StatusFilter(ctx.Query("status")) |
| 227 | if err != nil { |
| 228 | return nil, err |
| 229 | } |
| 230 |
| 231 | proxyType, err := parseV2ProxyTypeFilter(ctx.Query("type")) |
| 232 | if err != nil { |
| 233 | return nil, err |
| 234 | } |
| 235 | userFilter, filterByUser := queryValue(ctx, "user") |
| 236 | clientIDFilter := ctx.Query("clientID") |
| 237 | q := strings.ToLower(ctx.Query("q")) |
| 238 |
| 239 | stats := c.listV2ProxyStats(proxyType) |
| 240 | items := make([]model.V2ProxyResp, 0, len(stats)) |
| 241 | for _, ps := range stats { |
| 242 | resp := c.buildV2ProxyResp(ps) |
| 243 | if filterByUser && resp.User != userFilter { |
| 244 | continue |
| 245 | } |
| 246 | if clientIDFilter != "" && resp.ClientID != clientIDFilter { |
| 247 | continue |
| 248 | } |
| 249 | if !matchV2StatusFilter(resp.Status.State == "online", statusFilter) { |
| 250 | continue |
| 251 | } |
| 252 | if q != "" && !matchV2ProxyQuery(resp, q) { |
| 253 | continue |
| 254 | } |
| 255 | items = append(items, resp) |
| 256 | } |
| 257 |
| 258 | slices.SortFunc(items, func(a, b model.V2ProxyResp) int { |
| 259 | if v := cmp.Compare(a.Spec.Type, b.Spec.Type); v != 0 { |
| 260 | return v |
| 261 | } |
| 262 | return cmp.Compare(a.Name, b.Name) |
| 263 | }) |
| 264 |
| 265 | return buildV2PageResp(items, page, pageSize), nil |
| 266 | } |
| 267 |
| 268 | // /api/v2/proxies/{name} |
| 269 | func (c *Controller) APIV2ProxyDetail(ctx *httppkg.Context) (any, error) { |
| 270 | name, err := decodeV2PathParam(ctx, "name", "proxy name") |
| 271 | if err != nil { |
| 272 | return nil, err |
| 273 | } |
| 274 |
| 275 | ps := mem.StatsCollector.GetProxyByName(name) |
| 276 | if ps == nil { |
| 277 | return nil, httppkg.NewError(http.StatusNotFound, "no proxy info found") |
| 278 | } |
| 279 | return c.buildV2ProxyResp(ps), nil |
| 280 | } |
| 281 |
| 282 | // /api/v2/proxies/{name}/traffic |
| 283 | func (c *Controller) APIV2ProxyTraffic(ctx *httppkg.Context) (any, error) { |
| 284 | name, err := decodeV2PathParam(ctx, "name", "proxy name") |
| 285 | if err != nil { |
| 286 | return nil, err |
| 287 | } |
| 288 |
| 289 | proxyTrafficInfo := mem.StatsCollector.GetProxyTraffic(name) |
| 290 | if proxyTrafficInfo == nil { |
| 291 | return nil, httppkg.NewError(http.StatusNotFound, "no proxy info found") |
| 292 | } |
| 293 |
| 294 | return buildV2ProxyTrafficResp(name, proxyTrafficInfo, time.Now()), nil |
| 295 | } |
| 296 |
| 297 | func decodeV2PathParam(ctx *httppkg.Context, key string, label string) (string, error) { |
| 298 | raw := ctx.Param(key) |
| 299 | if raw == "" { |
| 300 | return "", fmt.Errorf("missing %s", label) |
| 301 | } |
| 302 | decoded, err := url.PathUnescape(raw) |
| 303 | if err != nil { |
| 304 | return "", httppkg.NewError(http.StatusBadRequest, fmt.Sprintf("invalid %s", label)) |
| 305 | } |
| 306 | return decoded, nil |
| 307 | } |
| 308 |
| 309 | func getOrCreateV2User(items map[string]*model.V2UserResp, user string) *model.V2UserResp { |
| 310 | item, ok := items[user] |
| 311 | if !ok { |
| 312 | item = &model.V2UserResp{User: user} |
| 313 | items[user] = item |
| 314 | } |
| 315 | return item |
| 316 | } |
| 317 |
| 318 | func parseV2PageParams(ctx *httppkg.Context) (int, int, error) { |
| 319 | page, err := parseV2PositiveInt(ctx.Query("page"), defaultV2Page, "page") |
| 320 | if err != nil { |
| 321 | return 0, 0, err |
| 322 | } |
| 323 | pageSize, err := parseV2PositiveInt(ctx.Query("pageSize"), defaultV2PageSize, "pageSize") |
| 324 | if err != nil { |
| 325 | return 0, 0, err |
| 326 | } |
| 327 | if pageSize > maxV2PageSize { |
| 328 | return 0, 0, httppkg.NewError(http.StatusBadRequest, fmt.Sprintf("pageSize must be between 1 and %d", maxV2PageSize)) |
| 329 | } |
| 330 | if page > math.MaxInt/pageSize { |
| 331 | return 0, 0, httppkg.NewError(http.StatusBadRequest, "page is too large") |
| 332 | } |
| 333 | return page, pageSize, nil |
| 334 | } |
| 335 |
| 336 | func parseV2PositiveInt(raw string, defaultValue int, name string) (int, error) { |
| 337 | if raw == "" { |
| 338 | return defaultValue, nil |
| 339 | } |
| 340 | value, err := strconv.Atoi(raw) |
| 341 | if err != nil || value < 1 { |
| 342 | return 0, httppkg.NewError(http.StatusBadRequest, fmt.Sprintf("%s must be a positive integer", name)) |
| 343 | } |
| 344 | return value, nil |
| 345 | } |
| 346 |
| 347 | func parseV2StatusFilter(raw string) (string, error) { |
| 348 | status := strings.ToLower(raw) |
| 349 | switch status { |
| 350 | case "", "all", "online", "offline": |
| 351 | return status, nil |
| 352 | default: |
| 353 | return "", httppkg.NewError(http.StatusBadRequest, "status must be one of all, online, offline") |
| 354 | } |
| 355 | } |
| 356 |
| 357 | func parseV2ProxyTypeFilter(raw string) (string, error) { |
| 358 | proxyType := strings.ToLower(raw) |
| 359 | if proxyType == "" { |
| 360 | return "", nil |
| 361 | } |
| 362 | if slices.Contains(apiV2ProxyTypes, proxyType) { |
| 363 | return proxyType, nil |
| 364 | } |
| 365 | return "", httppkg.NewError(http.StatusBadRequest, "type must be one of tcp, udp, http, https, tcpmux, stcp, xtcp, sudp") |
| 366 | } |
| 367 |
| 368 | func parseV2SystemPruneType(raw string) (string, error) { |
| 369 | pruneType := strings.ToLower(raw) |
| 370 | switch pruneType { |
| 371 | case "": |
| 372 | return "", httppkg.NewError(http.StatusBadRequest, "type is required") |
| 373 | case v2SystemPruneTypeOfflineProxies: |
| 374 | return pruneType, nil |
| 375 | default: |
| 376 | return "", httppkg.NewError(http.StatusBadRequest, "type must be one of offline_proxies") |
| 377 | } |
| 378 | } |
| 379 |
| 380 | func matchV2StatusFilter(online bool, filter string) bool { |
| 381 | switch filter { |
| 382 | case "", "all": |
| 383 | return true |
| 384 | case "online": |
| 385 | return online |
| 386 | case "offline": |
| 387 | return !online |
| 388 | default: |
| 389 | return true |
| 390 | } |
| 391 | } |
| 392 |
| 393 | func buildV2PageResp[T any](items []T, page, pageSize int) model.V2PageResp[T] { |
| 394 | total := len(items) |
| 395 | return model.V2PageResp[T]{ |
| 396 | Total: total, |
| 397 | Page: page, |
| 398 | PageSize: pageSize, |
| 399 | Items: paginateV2Items(items, page, pageSize), |
| 400 | } |
| 401 | } |
| 402 |
| 403 | func paginateV2Items[T any](items []T, page, pageSize int) []T { |
| 404 | start := (page - 1) * pageSize |
| 405 | if start >= len(items) { |
| 406 | return []T{} |
| 407 | } |
| 408 | end := min(start+pageSize, len(items)) |
| 409 | return items[start:end] |
| 410 | } |
| 411 |
| 412 | func queryValue(ctx *httppkg.Context, key string) (string, bool) { |
| 413 | values, ok := ctx.Req.URL.Query()[key] |
| 414 | if !ok { |
| 415 | return "", false |
| 416 | } |
| 417 | if len(values) == 0 { |
| 418 | return "", true |
| 419 | } |
| 420 | return values[0], true |
| 421 | } |
| 422 |
| 423 | func matchV2ClientQuery(item model.ClientInfoResp, q string) bool { |
| 424 | return containsV2Query(q, |
| 425 | item.Key, |
| 426 | item.User, |
| 427 | item.ClientID, |
| 428 | item.RunID, |
| 429 | item.Version, |
| 430 | item.WireProtocol, |
| 431 | item.Hostname, |
| 432 | item.ClientIP, |
| 433 | ) |
| 434 | } |
| 435 |
| 436 | func matchV2ProxyQuery(item model.V2ProxyResp, q string) bool { |
| 437 | values := []string{ |
| 438 | item.Name, |
| 439 | item.Spec.Type, |
| 440 | item.User, |
| 441 | item.ClientID, |
| 442 | item.Status.State, |
| 443 | } |
| 444 |
| 445 | switch item.Spec.Type { |
| 446 | case string(v1.ProxyTypeTCP): |
| 447 | if item.Spec.TCP != nil && item.Spec.TCP.RemotePort != nil { |
| 448 | values = append(values, strconv.Itoa(*item.Spec.TCP.RemotePort)) |
| 449 | } |
| 450 | case string(v1.ProxyTypeUDP): |
| 451 | if item.Spec.UDP != nil && item.Spec.UDP.RemotePort != nil { |
| 452 | values = append(values, strconv.Itoa(*item.Spec.UDP.RemotePort)) |
| 453 | } |
| 454 | case string(v1.ProxyTypeHTTP): |
| 455 | if item.Spec.HTTP != nil { |
| 456 | values = append(values, item.Spec.HTTP.CustomDomains...) |
| 457 | values = append(values, item.Spec.HTTP.Subdomain) |
| 458 | } |
| 459 | case string(v1.ProxyTypeHTTPS): |
| 460 | if item.Spec.HTTPS != nil { |
| 461 | values = append(values, item.Spec.HTTPS.CustomDomains...) |
| 462 | values = append(values, item.Spec.HTTPS.Subdomain) |
| 463 | } |
| 464 | case string(v1.ProxyTypeTCPMUX): |
| 465 | if item.Spec.TCPMux != nil { |
| 466 | values = append(values, item.Spec.TCPMux.CustomDomains...) |
| 467 | values = append(values, item.Spec.TCPMux.Subdomain) |
| 468 | } |
| 469 | } |
| 470 |
| 471 | return containsV2Query(q, values...) |
| 472 | } |
| 473 |
| 474 | func containsV2Query(q string, values ...string) bool { |
| 475 | for _, value := range values { |
| 476 | if strings.Contains(strings.ToLower(value), q) { |
| 477 | return true |
| 478 | } |
| 479 | } |
| 480 | return false |
| 481 | } |
| 482 |
| 483 | func (c *Controller) listV2ProxyStats(proxyType string) []*mem.ProxyStats { |
| 484 | if proxyType != "" { |
| 485 | return mem.StatsCollector.GetProxiesByType(proxyType) |
| 486 | } |
| 487 |
| 488 | items := make([]*mem.ProxyStats, 0) |
| 489 | for _, t := range apiV2ProxyTypes { |
| 490 | items = append(items, mem.StatsCollector.GetProxiesByType(t)...) |
| 491 | } |
| 492 | return items |
| 493 | } |
| 494 |
| 495 | func buildV2ProxyTrafficResp(name string, traffic *mem.ProxyTrafficInfo, now time.Time) model.V2ProxyTrafficResp { |
| 496 | history := make([]model.V2ProxyTrafficPointResp, 0, v2ProxyTrafficDefaultDays) |
| 497 | for age := v2ProxyTrafficDefaultDays - 1; age >= 0; age-- { |
| 498 | history = append(history, model.V2ProxyTrafficPointResp{ |
| 499 | Date: now.AddDate(0, 0, -age).Format(time.DateOnly), |
| 500 | TrafficIn: v2TrafficValueAt(traffic.TrafficIn, age), |
| 501 | TrafficOut: v2TrafficValueAt(traffic.TrafficOut, age), |
| 502 | }) |
| 503 | } |
| 504 |
| 505 | return model.V2ProxyTrafficResp{ |
| 506 | Name: name, |
| 507 | Unit: v2ProxyTrafficUnit, |
| 508 | Granularity: v2ProxyTrafficGranularity, |
| 509 | History: history, |
| 510 | } |
| 511 | } |
| 512 |
| 513 | func v2TrafficValueAt(values []int64, todayFirstIndex int) int64 { |
| 514 | if todayFirstIndex >= len(values) { |
| 515 | return 0 |
| 516 | } |
| 517 | return values[todayFirstIndex] |
| 518 | } |
| 519 |
| 520 | func (c *Controller) buildV2ClientStatus(info registry.ClientInfo) model.V2ClientStatusResp { |
| 521 | status := model.V2ClientStatusResp{State: "offline"} |
| 522 | if info.Online { |
| 523 | status.State = "online" |
| 524 | } |
| 525 |
| 526 | user := info.User |
| 527 | clientID := info.ClientID() |
| 528 | for _, ps := range c.listV2ProxyStats("") { |
| 529 | if ps.User != user || ps.ClientID != clientID { |
| 530 | continue |
| 531 | } |
| 532 | status.CurConns += ps.CurConns |
| 533 | status.ProxyCount++ |
| 534 | } |
| 535 | return status |
| 536 | } |
| 537 |
| 538 | func (c *Controller) buildV2ProxyResp(ps *mem.ProxyStats) model.V2ProxyResp { |
| 539 | state := "offline" |
| 540 | var cfg v1.ProxyConfigurer |
| 541 | if c.pxyManager != nil { |
| 542 | if pxy, ok := c.pxyManager.GetByName(ps.Name); ok { |
| 543 | state = "online" |
| 544 | cfg = pxy.GetConfigurer() |
| 545 | } |
| 546 | } |
| 547 |
| 548 | return model.V2ProxyResp{ |
| 549 | Name: ps.Name, |
| 550 | User: ps.User, |
| 551 | ClientID: ps.ClientID, |
| 552 | Spec: buildV2ProxySpec(ps.Type, cfg), |
| 553 | Status: model.V2ProxyStatusResp{ |
| 554 | State: state, |
| 555 | TodayTrafficIn: ps.TodayTrafficIn, |
| 556 | TodayTrafficOut: ps.TodayTrafficOut, |
| 557 | CurConns: ps.CurConns, |
| 558 | LastStartAt: ps.LastStartAt, |
| 559 | LastCloseAt: ps.LastCloseAt, |
| 560 | }, |
| 561 | } |
| 562 | } |
| 563 |
| 564 | func buildV2ProxySpec(proxyType string, cfg v1.ProxyConfigurer) model.V2ProxySpec { |
| 565 | spec := model.V2ProxySpec{Type: proxyType} |
| 566 |
| 567 | switch proxyType { |
| 568 | case string(v1.ProxyTypeTCP): |
| 569 | block := &model.V2TCPProxySpec{} |
| 570 | if c, ok := cfg.(*v1.TCPProxyConfig); ok { |
| 571 | block.V2ProxyBaseSpec = buildV2ProxyBaseSpec(c.GetBaseConfig()) |
| 572 | block.RemotePort = &c.RemotePort |
| 573 | } |
| 574 | spec.TCP = block |
| 575 | case string(v1.ProxyTypeUDP): |
| 576 | block := &model.V2UDPProxySpec{} |
| 577 | if c, ok := cfg.(*v1.UDPProxyConfig); ok { |
| 578 | block.V2ProxyBaseSpec = buildV2ProxyBaseSpec(c.GetBaseConfig()) |
| 579 | block.RemotePort = &c.RemotePort |
| 580 | } |
| 581 | spec.UDP = block |
| 582 | case string(v1.ProxyTypeHTTP): |
| 583 | block := &model.V2HTTPProxySpec{} |
| 584 | if c, ok := cfg.(*v1.HTTPProxyConfig); ok { |
| 585 | block.V2ProxyBaseSpec = buildV2ProxyBaseSpec(c.GetBaseConfig()) |
| 586 | block.CustomDomains = slices.Clone(c.CustomDomains) |
| 587 | block.Subdomain = c.SubDomain |
| 588 | block.Locations = slices.Clone(c.Locations) |
| 589 | block.HostHeaderRewrite = c.HostHeaderRewrite |
| 590 | } |
| 591 | spec.HTTP = block |
| 592 | case string(v1.ProxyTypeHTTPS): |
| 593 | block := &model.V2HTTPSProxySpec{} |
| 594 | if c, ok := cfg.(*v1.HTTPSProxyConfig); ok { |
| 595 | block.V2ProxyBaseSpec = buildV2ProxyBaseSpec(c.GetBaseConfig()) |
| 596 | block.CustomDomains = slices.Clone(c.CustomDomains) |
| 597 | block.Subdomain = c.SubDomain |
| 598 | } |
| 599 | spec.HTTPS = block |
| 600 | case string(v1.ProxyTypeTCPMUX): |
| 601 | block := &model.V2TCPMuxProxySpec{} |
| 602 | if c, ok := cfg.(*v1.TCPMuxProxyConfig); ok { |
| 603 | block.V2ProxyBaseSpec = buildV2ProxyBaseSpec(c.GetBaseConfig()) |
| 604 | block.CustomDomains = slices.Clone(c.CustomDomains) |
| 605 | block.Subdomain = c.SubDomain |
| 606 | block.Multiplexer = c.Multiplexer |
| 607 | block.RouteByHTTPUser = c.RouteByHTTPUser |
| 608 | } |
| 609 | spec.TCPMux = block |
| 610 | case string(v1.ProxyTypeSTCP): |
| 611 | block := &model.V2STCPProxySpec{} |
| 612 | if c, ok := cfg.(*v1.STCPProxyConfig); ok { |
| 613 | block.V2ProxyBaseSpec = buildV2ProxyBaseSpec(c.GetBaseConfig()) |
| 614 | } |
| 615 | spec.STCP = block |
| 616 | case string(v1.ProxyTypeSUDP): |
| 617 | block := &model.V2SUDPProxySpec{} |
| 618 | if c, ok := cfg.(*v1.SUDPProxyConfig); ok { |
| 619 | block.V2ProxyBaseSpec = buildV2ProxyBaseSpec(c.GetBaseConfig()) |
| 620 | } |
| 621 | spec.SUDP = block |
| 622 | case string(v1.ProxyTypeXTCP): |
| 623 | block := &model.V2XTCPProxySpec{} |
| 624 | if c, ok := cfg.(*v1.XTCPProxyConfig); ok { |
| 625 | block.V2ProxyBaseSpec = buildV2ProxyBaseSpec(c.GetBaseConfig()) |
| 626 | } |
| 627 | spec.XTCP = block |
| 628 | } |
| 629 |
| 630 | return spec |
| 631 | } |
| 632 |
| 633 | func buildV2ProxyBaseSpec(base *v1.ProxyBaseConfig) model.V2ProxyBaseSpec { |
| 634 | return model.V2ProxyBaseSpec{ |
| 635 | Annotations: maps.Clone(base.Annotations), |
| 636 | Metadatas: maps.Clone(base.Metadatas), |
| 637 | Transport: &model.V2ProxyTransportSpec{ |
| 638 | UseEncryption: base.Transport.UseEncryption, |
| 639 | UseCompression: base.Transport.UseCompression, |
| 640 | BandwidthLimit: base.Transport.BandwidthLimit.String(), |
| 641 | BandwidthLimitMode: base.Transport.BandwidthLimitMode, |
| 642 | }, |
| 643 | LoadBalancer: &model.V2ProxyLoadBalancerSpec{ |
| 644 | Group: base.LoadBalancer.Group, |
| 645 | }, |
| 646 | } |
| 647 | } |