github.com/xxf098/lite-proxy@v0.15.1-0.20230422081941-12c69f323218/web/msg.go (about)

     1  package web
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"net"
     7  	"strconv"
     8  	"strings"
     9  
    10  	"github.com/xxf098/lite-proxy/config"
    11  	"github.com/xxf098/lite-proxy/download"
    12  )
    13  
    14  const (
    15  	TROJAN_DEFAULT_GROUP    = "TrojanProvider"
    16  	V2RAY_DEFAULT_GROUP     = "V2RayProvider"
    17  	SS_DEFAULT_GROUP        = "SSProvider"
    18  	SSR_DEFAULT_GROUP       = "SSRProvider"
    19  	SPEEDTEST_ERROR_NONODES = "{\"info\":\"error\",\"reason\":\"nonodes\"}\n"
    20  )
    21  
    22  type Message struct {
    23  	ID       int       `json:"id"`
    24  	Info     string    `json:"info"`
    25  	Remarks  string    `json:"remarks,omitempty"`
    26  	Server   string    `json:"server,omitempty"`
    27  	Group    string    `json:"group,omitempty"`
    28  	Ping     int64     `json:"ping,omitempty"`
    29  	Lost     string    `json:"lost,omitempty"`
    30  	Speed    string    `json:"speed,omitempty"`
    31  	MaxSpeed string    `json:"maxspeed,omitempty"`
    32  	Traffic  int64     `json:"traffic,omitempty"`
    33  	Link     string    `json:"link,omitempty"`
    34  	Protocol string    `json:"protocol,omitempty"`
    35  	PicData  string    `json:"data,omitempty"`
    36  	Servers  []Message `json:"servers,omitempty"`
    37  }
    38  
    39  func GetRemarks(link string) (string, string, error) {
    40  	cfg, err := config.Link2Config(link)
    41  	if err != nil {
    42  		return "", "", err
    43  	}
    44  	return cfg.Protocol, cfg.Remarks, err
    45  }
    46  
    47  func gotserverMsg(id int, link string, groupName string) []byte {
    48  	msg := Message{ID: id, Info: "gotserver"}
    49  	cfg, err := config.Link2Config(link)
    50  	if err == nil {
    51  		msg.Group = groupName
    52  		msg.Remarks = cfg.Remarks
    53  		msg.Server = net.JoinHostPort(cfg.Server, strconv.Itoa(cfg.Port))
    54  		msg.Protocol = cfg.Protocol
    55  		if cfg.Protocol == "vmess" && cfg.Net != "" {
    56  			msg.Protocol = fmt.Sprintf("%s/%s", cfg.Protocol, cfg.Net)
    57  		}
    58  		msg.Link = link
    59  	}
    60  	b, _ := json.Marshal(msg)
    61  	return b
    62  }
    63  
    64  func gotserversMsg(startID int, links []string, groupName string) []byte {
    65  	servers := Message{ID: startID, Info: "gotservers"}
    66  	for i, link := range links {
    67  		id := startID + i
    68  		msg := Message{ID: id, Info: "gotserver"}
    69  		cfg, err := config.Link2Config(link)
    70  		if err == nil {
    71  			msg.Group = groupName
    72  			msg.Remarks = cfg.Remarks
    73  			msg.Server = net.JoinHostPort(cfg.Server, strconv.Itoa(cfg.Port))
    74  			msg.Protocol = cfg.Protocol
    75  			if (cfg.Protocol == "vmess" || cfg.Protocol == "trojan") && cfg.Net != "" {
    76  				msg.Protocol = fmt.Sprintf("%s/%s", cfg.Protocol, cfg.Net)
    77  			}
    78  			msg.Link = link
    79  		}
    80  		servers.Servers = append(servers.Servers, msg)
    81  	}
    82  	b, _ := json.Marshal(servers)
    83  	return b
    84  }
    85  
    86  func getMsgByte(id int, typ string, option ...interface{}) []byte {
    87  	msg := Message{ID: id, Info: typ}
    88  	switch typ {
    89  	case "gotping":
    90  		msg.Lost = "0.00%"
    91  		if len(option) > 0 {
    92  			if v, ok := option[0].(int64); ok {
    93  				msg.Ping = v
    94  			}
    95  		}
    96  
    97  	case "gotspeed":
    98  		var speed int64
    99  		var maxspeed int64
   100  		var traffic int64
   101  		if len(option) > 1 {
   102  			if v, ok := option[0].(int64); ok {
   103  				speed = v
   104  			}
   105  			if v, ok := option[1].(int64); ok {
   106  				maxspeed = v
   107  			}
   108  			if len(option) > 2 {
   109  				if v, ok := option[2].(int64); ok {
   110  					traffic = v
   111  				}
   112  			}
   113  		}
   114  		msg.Speed = strings.TrimRight(download.ByteCountIEC(speed), "/s")
   115  		msg.MaxSpeed = strings.TrimRight(download.ByteCountIEC(maxspeed), "/s")
   116  		msg.Traffic = traffic
   117  		if speed < 1 {
   118  			msg.Speed = "N/A"
   119  		}
   120  		if maxspeed < 1 {
   121  			msg.MaxSpeed = "N/A"
   122  		}
   123  	case "picdata":
   124  		if len(option) > 0 {
   125  			if v, ok := option[0].(string); ok {
   126  				msg.PicData = v
   127  			}
   128  		}
   129  	}
   130  	b, _ := json.Marshal(msg)
   131  	return b
   132  }