clash_external.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. if encryption, ok := settings["encryption"].(string); ok {
  55. encryption = strings.TrimSpace(encryption)
  56. if encryption != "" && encryption != "none" {
  57. proxy["encryption"] = encryption
  58. }
  59. }
  60. case "trojan":
  61. server := firstServer(settings)
  62. if server == nil {
  63. return nil
  64. }
  65. proxy["type"] = "trojan"
  66. proxy["server"] = fmt.Sprint(server["address"])
  67. proxy["port"] = clashInt(server["port"])
  68. proxy["password"] = fmt.Sprint(server["password"])
  69. case "shadowsocks":
  70. server := firstServer(settings)
  71. if server == nil {
  72. server = settings
  73. }
  74. method, _ := server["method"].(string)
  75. if method == "" {
  76. return nil
  77. }
  78. proxy["type"] = "ss"
  79. proxy["server"] = fmt.Sprint(server["address"])
  80. proxy["port"] = clashInt(server["port"])
  81. proxy["cipher"] = method
  82. proxy["password"] = fmt.Sprint(server["password"])
  83. // No early return: the shared transport/security tail is what drops an
  84. // obfs node Clash cannot express, exactly as buildProxy does for inbounds.
  85. case "hysteria":
  86. return clashHysteriaFromExternal(settings, stream, name)
  87. case "wireguard":
  88. return clashWireguardFromExternal(settings, name)
  89. default:
  90. return nil
  91. }
  92. network, _ := stream["network"].(string)
  93. if !s.applyTransport(proxy, network, stream) {
  94. return nil
  95. }
  96. security, _ := stream["security"].(string)
  97. if !s.applySecurity(proxy, security, stream) {
  98. return nil
  99. }
  100. return proxy
  101. }
  102. func firstServer(settings map[string]any) map[string]any {
  103. servers, _ := settings["servers"].([]any)
  104. if len(servers) == 0 {
  105. return nil
  106. }
  107. server, _ := servers[0].(map[string]any)
  108. return server
  109. }
  110. func clashHysteriaFromExternal(settings, stream map[string]any, name string) map[string]any {
  111. hy, _ := stream["hysteriaSettings"].(map[string]any)
  112. auth := ""
  113. if hy != nil {
  114. auth, _ = hy["auth"].(string)
  115. }
  116. if auth == "" {
  117. return nil
  118. }
  119. proxy := map[string]any{
  120. "name": name,
  121. "type": "hysteria2",
  122. "server": fmt.Sprint(settings["address"]),
  123. "port": clashInt(settings["port"]),
  124. "password": auth,
  125. "udp": true,
  126. }
  127. if tls, _ := stream["tlsSettings"].(map[string]any); tls != nil {
  128. if sni, _ := tls["serverName"].(string); sni != "" {
  129. proxy["sni"] = sni
  130. }
  131. if alpn := clashStringList(tls["alpn"]); len(alpn) > 0 {
  132. proxy["alpn"] = alpn
  133. }
  134. if fp, _ := tls["fingerprint"].(string); fp != "" {
  135. proxy["client-fingerprint"] = fp
  136. }
  137. }
  138. return proxy
  139. }
  140. func clashWireguardFromExternal(settings map[string]any, name string) map[string]any {
  141. peers, _ := settings["peers"].([]any)
  142. if len(peers) == 0 {
  143. return nil
  144. }
  145. peer, _ := peers[0].(map[string]any)
  146. if peer == nil {
  147. return nil
  148. }
  149. host, port := splitClashHostPort(fmt.Sprint(peer["endpoint"]))
  150. if host == "" || port == 0 {
  151. return nil
  152. }
  153. proxy := map[string]any{
  154. "name": name,
  155. "type": "wireguard",
  156. "server": host,
  157. "port": port,
  158. "udp": true,
  159. }
  160. if sk, _ := settings["secretKey"].(string); sk != "" {
  161. proxy["private-key"] = sk
  162. }
  163. if pk, _ := peer["publicKey"].(string); pk != "" {
  164. proxy["public-key"] = pk
  165. }
  166. if psk, _ := peer["preSharedKey"].(string); psk != "" {
  167. proxy["pre-shared-key"] = psk
  168. }
  169. for _, addr := range clashStringList(settings["address"]) {
  170. ip := stripCIDR(addr)
  171. if strings.Contains(ip, ":") {
  172. proxy["ipv6"] = ip
  173. } else {
  174. proxy["ip"] = ip
  175. }
  176. }
  177. return proxy
  178. }
  179. func clashInt(v any) int {
  180. switch x := v.(type) {
  181. case int:
  182. return x
  183. case int64:
  184. return int(x)
  185. case float64:
  186. return int(x)
  187. case string:
  188. n, _ := strconv.Atoi(x)
  189. return n
  190. default:
  191. return 0
  192. }
  193. }
  194. func clashStringList(v any) []string {
  195. switch x := v.(type) {
  196. case []any:
  197. out := make([]string, 0, len(x))
  198. for _, item := range x {
  199. if s, ok := item.(string); ok && s != "" {
  200. out = append(out, s)
  201. }
  202. }
  203. return out
  204. case []string:
  205. return x
  206. case string:
  207. if x == "" {
  208. return nil
  209. }
  210. return strings.Split(x, ",")
  211. default:
  212. return nil
  213. }
  214. }
  215. func stripCIDR(addr string) string {
  216. if before, _, ok := strings.Cut(addr, "/"); ok {
  217. return before
  218. }
  219. return addr
  220. }
  221. func splitClashHostPort(endpoint string) (string, int) {
  222. endpoint = strings.TrimSpace(endpoint)
  223. i := strings.LastIndex(endpoint, ":")
  224. if i < 0 {
  225. return endpoint, 0
  226. }
  227. host := strings.Trim(endpoint[:i], "[]")
  228. port, _ := strconv.Atoi(endpoint[i+1:])
  229. return host, port
  230. }