1
0

clash_external.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. package sub
  2. import (
  3. "fmt"
  4. "strconv"
  5. "strings"
  6. )
  7. // clashProxyFromExternal converts a pasted share link into a mihomo/Clash proxy
  8. // entry, or nil when Clash can't represent it — the same gate getProxies runs.
  9. func (s *SubClashService) clashProxyFromExternal(rawLink, name string) map[string]any {
  10. ob := parseExternalLink(rawLink)
  11. if ob == nil {
  12. return nil
  13. }
  14. protocol, _ := ob["protocol"].(string)
  15. settings, _ := ob["settings"].(map[string]any)
  16. stream, _ := ob["streamSettings"].(map[string]any)
  17. if stream == nil {
  18. stream = map[string]any{}
  19. }
  20. if settings == nil {
  21. return nil
  22. }
  23. proxy := map[string]any{"name": name, "udp": true}
  24. switch protocol {
  25. case "vmess":
  26. vnext, _ := settings["vnext"].([]any)
  27. if len(vnext) == 0 {
  28. return nil
  29. }
  30. vn, _ := vnext[0].(map[string]any)
  31. users, _ := vn["users"].([]any)
  32. if vn == nil || len(users) == 0 {
  33. return nil
  34. }
  35. user, _ := users[0].(map[string]any)
  36. proxy["type"] = "vmess"
  37. proxy["server"] = fmt.Sprint(vn["address"])
  38. proxy["port"] = clashInt(vn["port"])
  39. proxy["uuid"] = fmt.Sprint(user["id"])
  40. proxy["alterId"] = 0
  41. cipher, _ := user["security"].(string)
  42. if cipher == "" {
  43. cipher = "auto"
  44. }
  45. proxy["cipher"] = cipher
  46. case "vless":
  47. proxy["type"] = "vless"
  48. proxy["server"] = fmt.Sprint(settings["address"])
  49. proxy["port"] = clashInt(settings["port"])
  50. proxy["uuid"] = fmt.Sprint(settings["id"])
  51. if flow, _ := settings["flow"].(string); flow != "" {
  52. proxy["flow"] = flow
  53. }
  54. case "trojan":
  55. server := firstServer(settings)
  56. if server == nil {
  57. return nil
  58. }
  59. proxy["type"] = "trojan"
  60. proxy["server"] = fmt.Sprint(server["address"])
  61. proxy["port"] = clashInt(server["port"])
  62. proxy["password"] = fmt.Sprint(server["password"])
  63. case "shadowsocks":
  64. server := firstServer(settings)
  65. if server == nil {
  66. server = settings
  67. }
  68. method, _ := server["method"].(string)
  69. if method == "" {
  70. return nil
  71. }
  72. proxy["type"] = "ss"
  73. proxy["server"] = fmt.Sprint(server["address"])
  74. proxy["port"] = clashInt(server["port"])
  75. proxy["cipher"] = method
  76. proxy["password"] = fmt.Sprint(server["password"])
  77. // No early return: the shared transport/security tail is what drops an
  78. // obfs node Clash cannot express, exactly as buildProxy does for inbounds.
  79. case "hysteria":
  80. return clashHysteriaFromExternal(settings, stream, name)
  81. case "wireguard":
  82. return clashWireguardFromExternal(settings, name)
  83. default:
  84. return nil
  85. }
  86. network, _ := stream["network"].(string)
  87. if !s.applyTransport(proxy, network, stream) {
  88. return nil
  89. }
  90. security, _ := stream["security"].(string)
  91. if !s.applySecurity(proxy, security, stream) {
  92. return nil
  93. }
  94. return proxy
  95. }
  96. func firstServer(settings map[string]any) map[string]any {
  97. servers, _ := settings["servers"].([]any)
  98. if len(servers) == 0 {
  99. return nil
  100. }
  101. server, _ := servers[0].(map[string]any)
  102. return server
  103. }
  104. func clashHysteriaFromExternal(settings, stream map[string]any, name string) map[string]any {
  105. hy, _ := stream["hysteriaSettings"].(map[string]any)
  106. auth := ""
  107. if hy != nil {
  108. auth, _ = hy["auth"].(string)
  109. }
  110. if auth == "" {
  111. return nil
  112. }
  113. proxy := map[string]any{
  114. "name": name,
  115. "type": "hysteria2",
  116. "server": fmt.Sprint(settings["address"]),
  117. "port": clashInt(settings["port"]),
  118. "password": auth,
  119. "udp": true,
  120. }
  121. if tls, _ := stream["tlsSettings"].(map[string]any); tls != nil {
  122. if sni, _ := tls["serverName"].(string); sni != "" {
  123. proxy["sni"] = sni
  124. }
  125. if alpn := clashStringList(tls["alpn"]); len(alpn) > 0 {
  126. proxy["alpn"] = alpn
  127. }
  128. if fp, _ := tls["fingerprint"].(string); fp != "" {
  129. proxy["client-fingerprint"] = fp
  130. }
  131. }
  132. return proxy
  133. }
  134. func clashWireguardFromExternal(settings map[string]any, name string) map[string]any {
  135. peers, _ := settings["peers"].([]any)
  136. if len(peers) == 0 {
  137. return nil
  138. }
  139. peer, _ := peers[0].(map[string]any)
  140. if peer == nil {
  141. return nil
  142. }
  143. host, port := splitClashHostPort(fmt.Sprint(peer["endpoint"]))
  144. if host == "" || port == 0 {
  145. return nil
  146. }
  147. proxy := map[string]any{
  148. "name": name,
  149. "type": "wireguard",
  150. "server": host,
  151. "port": port,
  152. "udp": true,
  153. }
  154. if sk, _ := settings["secretKey"].(string); sk != "" {
  155. proxy["private-key"] = sk
  156. }
  157. if pk, _ := peer["publicKey"].(string); pk != "" {
  158. proxy["public-key"] = pk
  159. }
  160. if psk, _ := peer["preSharedKey"].(string); psk != "" {
  161. proxy["pre-shared-key"] = psk
  162. }
  163. for _, addr := range clashStringList(settings["address"]) {
  164. ip := stripCIDR(addr)
  165. if strings.Contains(ip, ":") {
  166. proxy["ipv6"] = ip
  167. } else {
  168. proxy["ip"] = ip
  169. }
  170. }
  171. return proxy
  172. }
  173. func clashInt(v any) int {
  174. switch x := v.(type) {
  175. case int:
  176. return x
  177. case int64:
  178. return int(x)
  179. case float64:
  180. return int(x)
  181. case string:
  182. n, _ := strconv.Atoi(x)
  183. return n
  184. default:
  185. return 0
  186. }
  187. }
  188. func clashStringList(v any) []string {
  189. switch x := v.(type) {
  190. case []any:
  191. out := make([]string, 0, len(x))
  192. for _, item := range x {
  193. if s, ok := item.(string); ok && s != "" {
  194. out = append(out, s)
  195. }
  196. }
  197. return out
  198. case []string:
  199. return x
  200. case string:
  201. if x == "" {
  202. return nil
  203. }
  204. return strings.Split(x, ",")
  205. default:
  206. return nil
  207. }
  208. }
  209. func stripCIDR(addr string) string {
  210. if before, _, ok := strings.Cut(addr, "/"); ok {
  211. return before
  212. }
  213. return addr
  214. }
  215. func splitClashHostPort(endpoint string) (string, int) {
  216. endpoint = strings.TrimSpace(endpoint)
  217. i := strings.LastIndex(endpoint, ":")
  218. if i < 0 {
  219. return endpoint, 0
  220. }
  221. host := strings.Trim(endpoint[:i], "[]")
  222. port, _ := strconv.Atoi(endpoint[i+1:])
  223. return host, port
  224. }