net

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: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var HttpCmd = &cobra.Command{
	Use:   "http",
	Short: "Examine config[s] real delay using http request",
	Long:  ``,
	Run: func(cmd *cobra.Command, args []string) {
		examiner := xray.Examiner{
			Xs:                     xray.NewXrayService(verbose, insecureTLS),
			MaxDelay:               maximumAllowedDelay,
			Logs:                   false,
			ShowBody:               false,
			DoSpeedtest:            speedtest,
			DoIPInfo:               getIPInfo,
			TestEndpoint:           destURL,
			TestEndpointHttpMethod: httpMethod,
			SpeedtestAmount:        speedtestAmount,
		}

		if configLinksFile != "" {
			links := utils.ParseFileByNewline(configLinksFile)

			confRes := HttpTestMultipleConfigs(examiner, links, threadCount, true)

			if sortedByRealDelay {
				sort.Sort(confRes)
			}

			if outputType == "txt" {
				for _, v := range confRes {
					if v.Status == "passed" {
						validConfigs = append(validConfigs, v.ConfigLink)
					}
				}

				err := utils.WriteIntoFile(outputFile, []byte(strings.Join(validConfigs, "\n\n")))
				if err != nil {
					customlog.Printf(customlog.Failure, "Saving configs failed due to the error: %v\n", err)
					os.Exit(1)
				}
				customlog.Printf(customlog.Finished, "A total of %d working configurations have been saved to %s\n", len(validConfigs), outputFile)
			} else if outputType == "csv" {
				if outputFile == "valid.txt" {
					outputFile = "valid.csv"
				}

				out, err := gocsv.MarshalString(&confRes)
				if err != nil {
					customlog.Printf(customlog.Failure, "Saving configs failed due to the error: %v\n", err)
					os.Exit(1)
				}
				err = utils.WriteIntoFile(outputFile, []byte(out))
				if err != nil {
					customlog.Printf(customlog.Failure, "Saving configs failed due to the error: %v\n", err)
					os.Exit(1)
				}

				customlog.Printf(customlog.Finished, "A total of %d configurations have been saved to %s\n", len(confRes), outputFile)
			}

		} else {
			examiner.Logs = true
			res, err := examiner.ExamineConfig(configLink)
			if err != nil {
				customlog.Printf(customlog.Failure, "%s\n", err)
				return
			}

			if res.Status == "failed" {
				customlog.Printf(customlog.Failure, "Config didn't respond!\n")
			} else {
				customlog.Printf(customlog.Success, "Real Delay: %dms\n", res.Delay)
				if speedtest {
					customlog.Printf(customlog.Success, "Downloaded %dKB - Speed: %f mbps\n", speedtestAmount, res.DownloadSpeed)
					customlog.Printf(customlog.Success, "Uploaded %dKB - Speed: %f mbps\n", speedtestAmount, res.UploadSpeed)
				}
			}
		}

	},
}

HttpCmd represents the http command

View Source
var IcmpCmd = &cobra.Command{
	Use:   "icmp",
	Short: "PING or ICMP test config's host",
	Long:  ``,
	Run: func(cmd *cobra.Command, args []string) {
		parsed, err := xray.ParseXrayConfig(configLink)
		if err != nil {
			fmt.Fprintf(os.Stderr, "%v", err)
			os.Exit(1)
		}

		generalDetails := parsed.ConvertToGeneralConfig()
		if err != nil {
			fmt.Fprintf(os.Stderr, "%v", err)
			os.Exit(1)
		}

		icmp, err := network.NewIcmpPacket(generalDetails.Address, testCount)
		if err != nil {
			customlog.Printf(customlog.Failure, "%v\n", err)
		}

		err = icmp.MeasureReplyDelay()
		if err != nil {
			customlog.Printf(customlog.Failure, "MeasureReplyDelay Error: %v", err)
			os.Exit(1)
		}
	},
}

IcmpCmd represents the icmp command

View Source
var NetCmd = &cobra.Command{
	Use:   "net",
	Short: "Multiple network testing tool for one or multiple xray configs",
	Long:  ``,
	Run: func(cmd *cobra.Command, args []string) {
		cmd.Help()
	},
}

NetCmd represents the net command

View Source
var TcpCmd = &cobra.Command{
	Use:   "tcp",
	Short: "Examine TCP Connection delay to config's host",
	Long:  ``,
	Run: func(cmd *cobra.Command, args []string) {
		parsed, err := xray.ParseXrayConfig(configLink)
		if err != nil {
			customlog.Printf(customlog.Failure, "Couldn't parse the config!\n")
			os.Exit(1)
		}
		generalDetails := parsed.ConvertToGeneralConfig()

		tcpAddr, err := net.ResolveTCPAddr("tcp", generalDetails.Address+":"+generalDetails.Port)
		if err != nil {
			customlog.Printf(customlog.Failure, "ResolveTCPAddr failed: %v\n", err)
			os.Exit(1)
		}
		start := time.Now()
		conn, err := net.DialTCP("tcp", nil, tcpAddr)
		if err != nil {
			customlog.Printf(customlog.Failure, "Couldn't establish tcp conn! : %v\n", err)
			os.Exit(1)
		}
		customlog.Printf(customlog.Success, "Established TCP connection in %dms\n", time.Since(start).Milliseconds())
		conn.Close()
	},
}

TcpCmd represents the tcp command

Functions

This section is empty.

Types

type ConfigResults

type ConfigResults []*xray.Result

func HttpTestMultipleConfigs

func HttpTestMultipleConfigs(examiner xray.Examiner, links []string, threadCount uint16, verbose bool) ConfigResults

func (ConfigResults) Len

func (cResults ConfigResults) Len() int

func (ConfigResults) Less

func (cResults ConfigResults) Less(i, j int) bool

func (ConfigResults) Swap

func (cResults ConfigResults) Swap(i, j int)

Jump to

Keyboard shortcuts

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