-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b878be8
commit 012f777
Showing
2 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
package types | ||
|
||
import ( | ||
"bytes" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"text/template" | ||
) | ||
|
||
var ( | ||
configTemplate = strings.TrimSpace(` | ||
{ | ||
"api": { | ||
"services": [ | ||
"StatsService" | ||
], | ||
"tag": "api" | ||
}, | ||
"inbounds": [ | ||
{ | ||
"listen": "127.0.0.1", | ||
"port": {{ .API.Port }}, | ||
"protocol": "dokodemo-door", | ||
"settings": { | ||
"address": "127.0.0.1" | ||
}, | ||
"tag": "api" | ||
}, | ||
{ | ||
"listen": "127.0.0.1", | ||
"port": {{ .Proxy.Port }}, | ||
"protocol": "socks", | ||
"settings": { | ||
"ip": "127.0.0.1", | ||
"udp": true | ||
}, | ||
"sniffing": { | ||
"destOverride": [ | ||
"http", | ||
"tls" | ||
], | ||
"enabled": true | ||
}, | ||
"tag": "proxy" | ||
} | ||
], | ||
"log": { | ||
"loglevel": "none" | ||
}, | ||
"outbounds": [ | ||
{ | ||
"protocol": "vmess", | ||
"settings": { | ||
"vnext": [ | ||
{ | ||
"address": "{{ .VMess.Address }}", | ||
"port": {{ .VMess.Port }}, | ||
"users": [ | ||
{ | ||
"alterId": 0, | ||
"id": "{{ .VMess.ID }}" | ||
} | ||
] | ||
} | ||
] | ||
}, | ||
"streamSettings": { | ||
"network": "{{ .VMess.Transport }}" | ||
}, | ||
"tag": "vmess" | ||
} | ||
], | ||
"policy": { | ||
"levels": { | ||
"0": { | ||
"downlinkOnly": 0, | ||
"uplinkOnly": 0 | ||
} | ||
}, | ||
"system": { | ||
"statsOutboundDownlink": true, | ||
"statsOutboundUplink": true | ||
} | ||
}, | ||
"routing": { | ||
"rules": [ | ||
{ | ||
"inboundTag": [ | ||
"api" | ||
], | ||
"outboundTag": "api", | ||
"type": "field" | ||
} | ||
] | ||
}, | ||
"stats": {}, | ||
"transport": { | ||
"dsSettings": {}, | ||
"grpcSettings": {}, | ||
"gunSettings": {}, | ||
"httpSettings": {}, | ||
"kcpSettings": {}, | ||
"quicSettings": { | ||
"security": "chacha20-poly1305" | ||
}, | ||
"tcpSettings": {}, | ||
"wsSettings": {} | ||
} | ||
} | ||
`) | ||
) | ||
|
||
type Config struct { | ||
API struct { | ||
Port uint16 `json:"port"` | ||
} `json:"api"` | ||
Proxy struct { | ||
Port uint16 `json:"port"` | ||
} `json:"proxy"` | ||
VMess struct { | ||
Address string `json:"address"` | ||
ID string `json:"id"` | ||
Port uint16 `json:"port"` | ||
Transport string `json:"transport"` | ||
} `json:"vmess"` | ||
} | ||
|
||
func (c *Config) WriteToFile(dir string) error { | ||
t, err := template.New("config_v2ray_json").Parse(configTemplate) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var buf bytes.Buffer | ||
if err = t.Execute(&buf, c); err != nil { | ||
return err | ||
} | ||
|
||
cfgFilePath := filepath.Join(dir, ConfigFileName) | ||
return os.WriteFile(cfgFilePath, buf.Bytes(), 0600) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package types | ||
|
||
const ( | ||
ConfigFileName = "v2ray_config.json" | ||
) |