Files
Zakk 833af7dc74 refactor: use what the dependencies already provide
A read of the tree for home-grown code that a dependency or the platform
covers. Each item names what replaces it.

- IPv4 CIDR membership was a binary-string trie (infra/dataStructure/trie)
  fed with CIDRs rendered as bit strings; net.IPNet.Contains, which the
  same file already used for IPv6, does it. The trie package is gone.
- The response replay cache was an LRU with its own linked list, indexes
  and timeout strategy (infra/dataStructure/lru), of which one fixed-size
  instance existed; hashicorp/golang-lru/v2, already in go.mod, is that
  cache, typed. go mod tidy also dropped go-nanoid, which nothing imported.
- The HTTP client for subscription updates through the local proxy carried
  a CONNECT dialer of its own (pkg/util/proxyWithHttp); http.Transport's
  Proxy handles socks5:// and http:// URLs, resolves hostnames on the
  SOCKS proxy and uses CONNECT for https. Verified on the VM: a
  subscription update succeeds in both "pac" and "proxy" modes.
- core/dns/qtype.go listed nine type names in two switches before falling
  back to miekg/dns tables that contain the same names; dns.Type.String
  and dns.StringToType are the tables.
- The GUI tracked prefers-color-scheme with its own media query and
  toggled a theme-dark class nothing reads any more; Vuetify's "system"
  theme follows the OS itself. Verified in headless Chrome: auto follows
  the OS in both directions, an explicit choice overrides it.
- The AUR packages carried .INSTALL hooks whose only content was a
  coloured "Completed!" banner, duplicated in the deb/rpm after-install
  scripts; pacman and apt report that themselves. The hooks are gone, the
  release workflow stops copying them, and after_install/after_upgrade
  keep only daemon-reload and the restart.
- The tap release job downloaded eight artifacts in eight steps;
  actions/download-artifact takes a pattern with merge-multiple.
2026-09-20 16:40:34 +10:00

106 lines
2.0 KiB
Go

package common
import (
"errors"
"net/http"
"github.com/gin-gonic/gin"
lru "github.com/hashicorp/golang-lru/v2"
)
type Code string
type Resp struct {
Status int
Body gin.H
}
type codedResponse struct {
err error
coded *CodedError
}
func codedErrorBody(err error, coded *CodedError) gin.H {
body := gin.H{
"code": FAIL,
"message": err.Error(),
"data": nil,
"errorCode": coded.Code,
}
if coded.Params != nil {
body["params"] = coded.Params
}
return body
}
var RespCache, _ = lru.New[string, Resp](10)
const (
SUCCESS = "SUCCESS"
FAIL = "FAIL"
UNAUTHORIZED = "UNAUTHORIZED"
RequestIdHeader = "X-V2raya-Request-Id"
)
// 当code为FAIL时,data为string类型返回给前端的消息
func Response(ctx *gin.Context, code Code, data interface{}) (status int, body gin.H) {
if reqId := ctx.GetHeader(RequestIdHeader); reqId != "" {
if resp, ok := RespCache.Get(reqId); ok {
ctx.JSON(resp.Status, resp.Body)
return resp.Status, resp.Body
}
defer func() {
RespCache.Add(reqId, Resp{
Status: status,
Body: body,
})
}()
}
status = http.StatusOK
if code == UNAUTHORIZED {
code = FAIL
status = http.StatusUnauthorized
}
if code == FAIL {
switch data := data.(type) {
case string:
body = gin.H{
"code": code,
"message": data,
"data": nil,
}
case *CodedError:
body = codedErrorBody(data, data)
case codedResponse:
body = codedErrorBody(data.err, data.coded)
default:
body = gin.H{
"code": code,
"message": nil,
"data": data,
}
}
ctx.JSON(status, body)
return status, body
}
body = gin.H{
"code": code,
"message": nil,
"data": data,
}
ctx.JSON(status, body)
return status, body
}
func ResponseError(ctx *gin.Context, err error) {
var coded *CodedError
if errors.As(err, &coded) {
Response(ctx, FAIL, codedResponse{err: err, coded: coded})
return
}
Response(ctx, FAIL, err.Error())
}
func ResponseSuccess(ctx *gin.Context, data interface{}) {
Response(ctx, SUCCESS, data)
}