change src address strategy

This commit is contained in:
xvzc 2022-01-04 01:54:36 +09:00
parent 117e053751
commit b8f614d808
5 changed files with 10 additions and 33 deletions

View File

@ -11,13 +11,13 @@ import (
)
func main() {
src := flag.String("src", "127.0.0.1:8080", "source-ip:source-port")
port := flag.String("port", "8080", "port")
dns := flag.String("dns", "8.8.8.8", "DNS server")
debug := flag.Bool("debug", false, "true | false")
flag.Parse()
err := config.InitConfig(*src, *dns, *debug)
err := config.InitConfig(*port, *dns, *debug)
if err != nil {
os.Exit(1)
}

View File

@ -1,16 +1,12 @@
package config
import (
"errors"
"log"
"strings"
"sync"
"runtime"
)
type Config struct {
SrcIp string
SrcPort string
Port string
DNS string
OS string
Debug bool
@ -20,31 +16,13 @@ var config *Config
var once sync.Once
var err error
func tokenizeAddress(srcAddress string) (string, string, error) {
tokens := strings.Split(srcAddress, ":")
if len(tokens) < 2 {
return "", "", errors.New("Error while parsing source address: invalid format.")
}
ip := tokens[0]
port := tokens[1]
return ip, port, nil
}
func InitConfig(srcAddress string, dns string, debug bool) error {
func InitConfig(port string, dns string, debug bool) error {
err = nil
once.Do(func() {
ip, port, err := tokenizeAddress(srcAddress)
if err != nil {
log.Fatal(err)
return
}
config = &Config{
SrcIp : ip,
SrcPort : port,
Port : port,
DNS : dns,
OS : runtime.GOOS,
Debug : debug,

View File

@ -16,12 +16,12 @@ func SetOsProxy() error {
return err
}
_, err = exec.Command("sh", "-c", "networksetup -setwebproxy " + strings.TrimSpace(string(network)) + " " + GetConfig().SrcIp + " " + GetConfig().SrcPort).Output()
_, err = exec.Command("sh", "-c", "networksetup -setwebproxy " + strings.TrimSpace(string(network)) + " 127.0.0.1 " + GetConfig().Port).Output()
if err != nil {
return err
}
_, err = exec.Command("sh", "-c", "networksetup -setsecurewebproxy " + strings.TrimSpace(string(network)) + " " + GetConfig().SrcIp + " " + GetConfig().SrcPort).Output()
_, err = exec.Command("sh", "-c", "networksetup -setsecurewebproxy " + strings.TrimSpace(string(network)) + " 127.0.0.1 " + GetConfig().Port).Output()
if err != nil {
return err
}

View File

@ -10,7 +10,7 @@ import (
)
func Start() {
listener, err := net.Listen("tcp", ":" + config.GetConfig().SrcPort)
listener, err := net.Listen("tcp", ":" + config.GetConfig().Port)
if err != nil {
log.Fatal("Error creating listener: ", err)
os.Exit(1)

View File

@ -12,8 +12,7 @@ func PrintWelcome() {
pterm.DefaultBigText.WithLetters(cyan, purple).Render()
pterm.DefaultBulletList.WithItems([]pterm.BulletListItem{
{Level: 0, Text: "SOURCE IP : " + config.GetConfig().SrcIp},
{Level: 0, Text: "SOURCE PORT : " + config.GetConfig().SrcPort},
{Level: 0, Text: "PORT : " + config.GetConfig().Port},
{Level: 0, Text: "DNS : " + config.GetConfig().DNS},
}).Render()