proxy

package
v0.0.0-...-a0c44de Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 21, 2024 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ProxyCmd = &cobra.Command{
	Use:   "proxy",
	Short: "Creates proxy server",
	Long:  ``,
	Run: func(cmd *cobra.Command, args []string) {
		if len(args) < 1 && (!readConfigFromSTDIN && link == "" && configLinksFile == "") {
			cmd.Help()
			return
		}
		r := rand.New(rand.NewSource(time.Now().Unix()))
		var links []string
		var configs []xray.Protocol

		if configLinksFile != "" {

			links = utils.ParseFileByNewline(configLinksFile)

		} else if readConfigFromSTDIN {

			reader := bufio.NewReader(os.Stdin)
			fmt.Println("Reading config from STDIN:")
			link, _ = reader.ReadString('\n')
			links = append(links, link)
		}

		for _, c := range links {
			conf, err := xray.ParseXrayConfig(c)
			if err != nil {
				log.Println(color.RedString("Couldn't parse the config : %v", err))
			}
			configs = append(configs, conf)
		}

		inbound := &xray.Socks{
			Remark:  "Listener",
			Address: listenAddr,
			Port:    strconv.Itoa(int(listenPort)),
		}

		utils.ClearTerminal()

		xs := xray.NewXrayService(verbose, insecureTLS, xray.WithInbound(inbound))
		examiner := xray.Examiner{
			Xs:                     xray.NewXrayService(verbose, insecureTLS),
			MaxDelay:               2000,
			Logs:                   false,
			ShowBody:               false,
			DoSpeedtest:            false,
			DoIPInfo:               false,
			TestEndpoint:           "https://google.com/",
			TestEndpointHttpMethod: "GET",
		}

		fmt.Println(color.RedString("\n==========INBOUND=========="))
		fmt.Printf("%v", inbound.DetailsStr())
		fmt.Println(color.RedString("============================\n"))

		var xrayInstance *core.Instance = nil
		var err error

		signalChannel := make(chan os.Signal, 1)

		signal.Notify(signalChannel, os.Interrupt, syscall.SIGINT)

		go func() {

			sig := <-signalChannel

			customlog.Printf(customlog.Processing, "Received signal: %v\n", sig)

			customlog.Printf(customlog.Processing, "Closing xray service...")

			if xrayInstance != nil {
				err := xrayInstance.Close()
				if err != nil {

				}
			}

			os.Exit(0)
		}()

		if len(configs) > 1 {

			customlog.Printf(customlog.Processing, "Looking for a working outbound config...\n")

			connect := func() {
				var lastConfig string
				var currentConfig xray.Protocol = nil

				// Decide how many configs we are going to test
				var testCount int = 25
				if len(links) < 25 {
					testCount = len(links)
				}
				for currentConfig == nil {

					r.Shuffle(len(links), func(i, j int) { links[i], links[j] = links[j], links[i] })
					results := net.HttpTestMultipleConfigs(examiner, links[0:testCount-1], 50, false)
					sort.Sort(results)
					for _, v := range results {
						if v.ConfigLink != lastConfig {
							currentConfig = v.Protocol
							lastConfig = v.ConfigLink
							break
						}
					}
				}

				fmt.Println(color.RedString("==========OUTBOUND=========="))
				fmt.Printf("%v", currentConfig.DetailsStr())
				fmt.Println(color.RedString("============================"))

				xrayInstance, err = xs.MakeXrayInstance(currentConfig)
				if err != nil {
					customlog.Printf(customlog.Failure, "Error making a xray instance: %s\n", err.Error())

				}

				err = xrayInstance.Start()
				if err != nil {
					customlog.Printf(customlog.Failure, "Error starting xray instance: %s\n", err.Error())

				}
				customlog.Printf(customlog.Success, "Started listening for new connections...")
				fmt.Printf("\n")

				time.Sleep(time.Duration(interval) * time.Second)
			}

			for {

				connect()

				customlog.Printf(customlog.Processing, "Switching outbound connection...\n")

				if xrayInstance != nil {
					err = xrayInstance.Close()
					if err != nil {
						log.Fatalf(err.Error())
					}
				}
			}
		} else {

			outboundParsed, err := xray.ParseXrayConfig(link)
			if err != nil {
				log.Fatalf("Couldn't parse the config : %v", err)
			}
			fmt.Println(color.RedString("==========OUTBOUND=========="))
			fmt.Printf("%v", outboundParsed.DetailsStr())
			fmt.Println(color.RedString("============================"))

			xrayInstance, err = xs.MakeXrayInstance(outboundParsed)
			if err != nil {
				log.Fatalln(err.Error())
			}

			err = xrayInstance.Start()
			if err != nil {
				log.Fatalln(err.Error())
			}
			customlog.Printf(customlog.Success, "Started listening for new connections...")
			fmt.Printf("\n")
			select {}
		}

	},
}

ProxyCmd BotCmd represents the bot command

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL