github.com/laof/lite-speed-test@v0.0.0-20230930011949-1f39b7037845/config/config.go (about) 1 package config 2 3 import ( 4 "net/url" 5 "strconv" 6 "strings" 7 8 "github.com/laof/lite-speed-test/common" 9 "github.com/laof/lite-speed-test/outbound" 10 "github.com/laof/lite-speed-test/utils" 11 ) 12 13 func Link2Dialer(link string) (outbound.Dialer, error) { 14 matches, err := utils.CheckLink(link) 15 if err != nil { 16 return nil, err 17 } 18 var d outbound.Dialer 19 switch strings.ToLower(matches[1]) { 20 case "vmess": 21 if option, err1 := VmessLinkToVmessOption(link); err1 != nil { 22 return nil, err1 23 } else { 24 d, err = outbound.NewVmess(option) 25 } 26 case "trojan": 27 if option, err1 := TrojanLinkToTrojanOption(link); err1 != nil { 28 return nil, err1 29 } else { 30 d, err = outbound.NewTrojan(option) 31 } 32 case "ss": 33 if option, err1 := SSLinkToSSOption(link); err1 != nil { 34 return nil, err1 35 } else { 36 d, err = outbound.NewShadowSocks(option) 37 } 38 case "ssr": 39 if option, err1 := SSRLinkToSSROption(link); err1 != nil { 40 return nil, err1 41 } else { 42 d, err = outbound.NewShadowSocksR(option) 43 } 44 default: 45 return nil, common.NewError("Not Suported Link") 46 } 47 return d, err 48 } 49 50 type Config struct { 51 Protocol string 52 Remarks string 53 Server string 54 Net string // vmess net type 55 Port int 56 Password string 57 SNI string 58 } 59 60 func Link2Config(link string) (*Config, error) { 61 matches, err := utils.CheckLink(link) 62 if err != nil { 63 return nil, err 64 } 65 var cfg *Config = &Config{ 66 Protocol: "unknown", 67 Remarks: "", 68 Server: "127.0.0.1", 69 Port: 80, 70 } 71 switch strings.ToLower(matches[1]) { 72 case "vmess": 73 if cfgVmess, err1 := VmessLinkToVmessConfigIP(link, false); err1 != nil { 74 return nil, err1 75 } else { 76 remarks := cfgVmess.Ps 77 if len(remarks) < 1 { 78 remarks = cfgVmess.Add 79 } 80 cfg = &Config{ 81 Protocol: "vmess", 82 Remarks: remarks, 83 Server: cfgVmess.Add, 84 Port: cfgVmess.PortInt, 85 Net: cfgVmess.Net, 86 Password: cfgVmess.ID, 87 SNI: cfgVmess.Host, 88 } 89 } 90 case "trojan": 91 if cfgTrojan, err1 := TrojanLinkToTrojanOption(link); err1 != nil { 92 return nil, err1 93 } else { 94 cfg = &Config{ 95 Protocol: "trojan", 96 Remarks: cfgTrojan.Remarks, 97 Server: cfgTrojan.Server, 98 Port: cfgTrojan.Port, 99 Password: cfgTrojan.Password, 100 Net: cfgTrojan.Network, 101 SNI: cfgTrojan.SNI, 102 } 103 } 104 case "http": 105 if cfgHttp, err1 := HttpLinkToHttpOption(link); err1 != nil { 106 return nil, err1 107 } else { 108 cfg = &Config{ 109 Protocol: "http", 110 Remarks: cfgHttp.Remarks, 111 Server: cfgHttp.Server, 112 Port: cfgHttp.Port, 113 Password: cfgHttp.Password, 114 } 115 } 116 case "ss": 117 if cfgSS, err1 := SSLinkToSSOption(link); err1 != nil { 118 return nil, err1 119 } else { 120 cfg = &Config{ 121 Protocol: "ss", 122 Remarks: cfgSS.Remarks, 123 Server: cfgSS.Server, 124 Port: cfgSS.Port, 125 Password: cfgSS.Password, 126 } 127 } 128 case "ssr": 129 if cfgSSR, err1 := SSRLinkToSSROption(link); err1 != nil { 130 return nil, err1 131 } else { 132 cfg = &Config{ 133 Protocol: "ssr", 134 Remarks: cfgSSR.Remarks, 135 Server: cfgSSR.Server, 136 Port: cfgSSR.Port, 137 Password: cfgSSR.Password, 138 } 139 } 140 case "vless": 141 u, err := url.Parse(link) 142 if err != nil { 143 return nil, err 144 } 145 port, err := strconv.Atoi(u.Port()) 146 if err != nil { 147 return nil, err 148 } 149 password, _ := u.User.Password() 150 cfg = &Config{ 151 Protocol: "vless", 152 Remarks: u.Fragment, 153 Server: u.Host, 154 Port: port, 155 Password: password, 156 } 157 default: 158 return nil, common.NewError("Not Suported Link") 159 } 160 return cfg, err 161 }