clash_external.go 5.5 KB

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