Support SOCKS as outbound server. (#537)

This commit is contained in:
star-39 authored and GitHub committed 2022-05-15 19:52:19 +08:00
1 parent f7647c563e
commit ba251b2805
3 files changed
+218

No files matched your search

+88
View File
@@ -695,6 +695,52 @@
/>
</b-field>
</b-tab-item>
<b-tab-item label="SOCKS5">
<b-field label="Name" label-position="on-border">
<b-input
ref="socks5_name"
v-model="socks5.name"
:placeholder="$t('configureServer.servername')"
expanded
/>
</b-field>
<b-field label="Host" label-position="on-border">
<b-input
ref="socks5_host"
v-model="socks5.host"
required
placeholder="IP / HOST"
expanded
/>
</b-field>
<b-field label="Port" label-position="on-border">
<b-input
ref="socks5_port"
v-model="socks5.port"
required
:placeholder="$t('configureServer.port')"
type="number"
expanded
/>
</b-field>
<b-field label="Username" label-position="on-border">
<b-input
ref="socks5_username"
v-model="socks5.username"
:placeholder="$t('configureServer.username')"
expanded
/>
</b-field>
<b-field label="Password" label-position="on-border">
<b-input
ref="socks5_password"
v-model="socks5.password"
:placeholder="$t('configureServer.password')"
expanded
/>
</b-field>
</b-tab-item>
</b-tabs>
</section>
<footer v-if="!readonly" class="modal-card-foot flex-end">
@@ -801,6 +847,14 @@ export default {
protocol: "http",
name: ""
},
socks5: {
username: "",
password: "",
host: "",
port: "",
protocol: "socks5",
name: ""
},
tabChoice: 0,
presetFlows: [
"xtls-rprx-direct",
@@ -879,6 +933,11 @@ export default {
) {
this.http = this.resolveURL(res.data.data.sharingAddress);
this.tabChoice = 5;
} else if (
res.data.data.sharingAddress.toLowerCase().startsWith("socks5://")
) {
this.socks5 = this.resolveURL(res.data.data.sharingAddress);
this.tabChoice = 6;
}
this.$nextTick(() => {
if (this.readonly) {
@@ -1100,6 +1159,16 @@ export default {
protocol: u.protocol,
name: decodeURIComponent(u.hash)
};
} else if (url.toLowerCase().startsWith("socks5://")) {
let u = parseURL(url);
return {
username: decodeURIComponent(u.username),
password: decodeURIComponent(u.password),
host: u.host,
port: u.port,
protocol: u.protocol,
name: decodeURIComponent(u.hash)
};
}
return null;
},
@@ -1263,6 +1332,20 @@ export default {
});
}
return generateURL(tmp);
case "socks5":
tmp = {
protocol: "socks5",
host: srcObj.host,
port: srcObj.port,
hash: srcObj.name
};
if (srcObj.username && srcObj.password) {
Object.assign(tmp, {
username: srcObj.username,
password: srcObj.password
});
}
return generateURL(tmp);
}
return null;
},
@@ -1318,6 +1401,9 @@ export default {
if (this.tabChoice === 5 && !k.startsWith("http_")) {
continue;
}
if (this.tabChoice === 6 && !k.startsWith("socks5_")) {
continue;
}
let x = this.$refs[k];
if (!x) {
continue;
@@ -1366,6 +1452,8 @@ export default {
coded = this.generateURL(this.trojan);
} else if (this.tabChoice === 5) {
coded = this.generateURL(this.http);
} else if (this.tabChoice === 6) {
coded = this.generateURL(this.socks5);
}
this.$emit("submit", coded);
}
+129
View File
@@ -0,0 +1,129 @@
package serverObj
import (
"net"
"net/url"
"strconv"
"github.com/v2rayA/v2rayA/core/coreObj"
)
func init() {
FromLinkRegister("socks5", NewSOCKS)
EmptyRegister("socks5", func() (ServerObj, error) {
return new(SOCKS), nil
})
}
type SOCKS struct {
Name string `json:"name"`
Server string `json:"server"`
Port int `json:"port"`
Username string `json:"username"`
Password string `json:"password"`
Protocol string `json:"protocol"`
}
func NewSOCKS(link string) (ServerObj, error) {
return ParseSocksURL(link)
}
func ParseSocksURL(u string) (data *SOCKS, err error) {
t, err := url.Parse(u)
if err != nil {
return nil, InvalidParameterErr
}
port, err := strconv.Atoi(t.Port())
if err != nil {
return nil, InvalidParameterErr
}
data = &SOCKS{
Name: t.Fragment,
Server: t.Hostname(),
Port: port,
}
if t.User != nil && len(t.User.String()) > 0 {
data.Username = t.User.Username()
data.Password, _ = t.User.Password()
}
switch t.Scheme {
case "socks5":
data.Protocol = "socks5"
if data.Port == 0 {
data.Port = 1080
}
default:
data.Protocol = t.Scheme
}
return data, nil
}
func (h *SOCKS) Configuration(info PriorInfo) (c Configuration, err error) {
var users []coreObj.OutboundUser
if h.Username != "" && h.Password != "" {
users = []coreObj.OutboundUser{
{
User: h.Username,
Pass: h.Password,
},
}
}
o := coreObj.OutboundObject{
Tag: info.Tag,
Protocol: "socks",
Settings: coreObj.Settings{
Servers: []coreObj.Server{{
Address: h.Server,
Port: h.Port,
Users: users,
}},
},
}
return Configuration{
CoreOutbound: o,
PluginChain: "",
UDPSupport: true,
}, nil
}
func (h *SOCKS) ExportToURL() string {
var user *url.Userinfo
if h.Username != "" && h.Password != "" {
user = url.UserPassword(h.Username, h.Password)
}
u := &url.URL{
Scheme: h.Protocol,
User: user,
Host: net.JoinHostPort(h.Server, strconv.Itoa(h.Port)),
Fragment: h.Name,
}
return u.String()
}
func (h *SOCKS) NeedPlugin() bool {
return false
}
func (h *SOCKS) ProtoToShow() string {
return h.Protocol
}
func (h *SOCKS) GetProtocol() string {
return h.Protocol
}
func (h *SOCKS) GetHostname() string {
return h.Server
}
func (h *SOCKS) GetPort() int {
return h.Port
}
func (h *SOCKS) GetName() string {
return h.Name
}
func (h *SOCKS) SetName(name string) {
h.Name = name
}
+1
View File
@@ -39,6 +39,7 @@ func Import(url string, which *configure.Which) (err error) {
strings.HasPrefix(url, "trojan-go://") ||
strings.HasPrefix(url, "http-proxy://") ||
strings.HasPrefix(url, "https-proxy://") ||
strings.HasPrefix(url, "socks5://") ||
strings.HasPrefix(url, "http2://") {
var obj serverObj.ServerObj
obj, err = ResolveURL(url)