github.com/laof/lite-speed-test@v0.0.0-20230930011949-1f39b7037845/data/data.go (about)

     1  package data
     2  
     3  import (
     4  	"encoding/json"
     5  	"errors"
     6  	"io"
     7  	"net/http"
     8  	"strings"
     9  
    10  	"github.com/laof/lite-speed-test/ping"
    11  )
    12  
    13  type Nodes struct {
    14  	Url string
    15  	Max int
    16  }
    17  
    18  func (s Nodes) Get() (HttpData, error) {
    19  	var data HttpData
    20  	res, err := http.Get(s.Url)
    21  
    22  	if err != nil {
    23  		return data, err
    24  	}
    25  
    26  	defer res.Body.Close()
    27  	str, _ := io.ReadAll(res.Body)
    28  
    29  	err = json.Unmarshal(str, &data)
    30  
    31  	if err != nil {
    32  		return data, err
    33  	}
    34  
    35  	for i, item := range data.List {
    36  		name := reverseString(item.Name)
    37  		datetime := reverseString(item.Datetime)
    38  		txt := item.Data
    39  
    40  		for _, o := range data.Decode {
    41  			txt = strings.ReplaceAll(txt, o[1], o[0])
    42  		}
    43  
    44  		data.List[i].Name = name
    45  		data.List[i].Datetime = datetime
    46  		data.List[i].Data = txt
    47  	}
    48  	data.Decode = make([][]string, 0)
    49  	return data, nil
    50  }
    51  
    52  func (s Nodes) Test(data HttpData) (TestResult, error) {
    53  	var servers []string
    54  	var nodes []string
    55  	var max = s.Max
    56  	var all []string
    57  	result := TestResult{}
    58  	for _, item := range data.List {
    59  
    60  		if item.Data == "" {
    61  			continue
    62  		}
    63  
    64  		arr := strings.Split(item.Data, ",")
    65  		all = append(all, item.Name)
    66  		if max > 0 {
    67  			if len(arr) > max {
    68  				arr = arr[0:max]
    69  			}
    70  		}
    71  		nodes = append(nodes, arr...)
    72  		for range arr {
    73  			servers = append(servers, item.Name)
    74  		}
    75  	}
    76  
    77  	if len(nodes) == 0 {
    78  		return result, errors.New("no data(nodes)")
    79  	}
    80  
    81  	ssr := strings.Join(nodes, "\n")
    82  	res, err := ping.Test(ssr)
    83  	if err != nil {
    84  		return result, err
    85  	}
    86  
    87  	for _, n := range res.SuccessIndex {
    88  		result.SuccessNodes = append(result.SuccessNodes, nodes[n])
    89  	}
    90  
    91  	result.ErrorServers = getServerByIndex(res.ErrorIndex, servers)
    92  
    93  	for _, vvvv := range all {
    94  
    95  		if !hasValue(result.ErrorServers, vvvv) {
    96  			result.SuccessServers = append(result.SuccessServers, vvvv)
    97  		}
    98  
    99  	}
   100  
   101  	return result, nil
   102  }
   103  
   104  func hasValue(str []string, value string) bool {
   105  
   106  	for _, v := range str {
   107  		if v == value {
   108  			return true
   109  		}
   110  	}
   111  	return false
   112  }
   113  
   114  func removeDuplicates(arr []string) []string {
   115  
   116  	visited := make(map[string]bool)
   117  	var list []string
   118  	for _, str := range arr {
   119  		if visited[str] {
   120  			continue
   121  		}
   122  		visited[str] = true
   123  		list = append(list, str)
   124  	}
   125  	return list
   126  
   127  }
   128  
   129  func getServerByIndex(indexArr []int, service []string) []string {
   130  	var nodes []string
   131  
   132  	for _, n := range indexArr {
   133  
   134  		if !hasValue(nodes, service[n]) {
   135  			nodes = append(nodes, service[n])
   136  		}
   137  	}
   138  
   139  	return nodes
   140  
   141  }
   142  
   143  func reverseString(str string) string {
   144  	// 将字符串转换为字节切片
   145  	byteSlice := []byte(str)
   146  	length := len(byteSlice)
   147  
   148  	// 使用双指针进行字节切片的反转
   149  	for i := 0; i < length/2; i++ {
   150  		byteSlice[i], byteSlice[length-i-1] = byteSlice[length-i-1], byteSlice[i]
   151  	}
   152  
   153  	// 将字节切片转换为字符串并返回
   154  	return string(byteSlice)
   155  }
   156  
   157  type TestResult struct {
   158  	SuccessNodes   []string
   159  	ErrorServers   []string
   160  	SuccessServers []string
   161  	SuccessList    []List
   162  }
   163  
   164  type HttpData struct {
   165  	List   []List     `json:"list"`
   166  	Decode [][]string `json:"decode"`
   167  	Update string     `json:"update"`
   168  	Conf   []string   `json:"conf"`
   169  }
   170  
   171  type List struct {
   172  	Name     string `json:"name"`
   173  	Datetime string `json:"datetime"`
   174  	Length   int    `json:"length"`
   175  	Data     string `json:"data"`
   176  }