github.com/piotrnar/gocoin@v0.0.0-20240512203912-faa0448c5e96/client/usif/webui/network.go (about) 1 package webui 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "html" 7 "io/ioutil" 8 "net/http" 9 "runtime/debug" 10 "strconv" 11 "strings" 12 13 "github.com/piotrnar/gocoin/client/common" 14 "github.com/piotrnar/gocoin/client/network" 15 "github.com/piotrnar/gocoin/lib/btc" 16 ) 17 18 func p_net(w http.ResponseWriter, r *http.Request) { 19 if !ipchecker(r) { 20 return 21 } 22 23 net_page := load_template("net.html") 24 25 network.Mutex_net.Lock() 26 net_page = strings.Replace(net_page, "{LISTEN_TCP}", fmt.Sprint(common.IsListenTCP(), network.TCPServerStarted), 1) 27 net_page = strings.Replace(net_page, "{EXTERNAL_ADDR}", btc.NewNetAddr(network.BestExternalAddr()).String(), 1) 28 29 network.Mutex_net.Unlock() 30 31 d, _ := ioutil.ReadFile(common.GocoinHomeDir + "friends.txt") 32 net_page = strings.Replace(net_page, "{FRIENDS_TXT}", html.EscapeString(string(d)), 1) 33 34 write_html_head(w, r) 35 w.Write([]byte(net_page)) 36 write_html_tail(w) 37 } 38 39 func json_netcon(w http.ResponseWriter, r *http.Request) { 40 if !ipchecker(r) { 41 return 42 } 43 44 defer func() { 45 if r := recover(); r != nil { 46 err, ok := r.(error) 47 if !ok { 48 err = fmt.Errorf("pkg: %v", r) 49 } 50 fmt.Println("json_netcon recovered:", err.Error()) 51 fmt.Println(string(debug.Stack())) 52 } 53 }() 54 55 network.Mutex_net.Lock() 56 defer network.Mutex_net.Unlock() 57 58 net_cons := make([]network.ConnInfo, len(network.OpenCons)) 59 tmp, _ := network.GetSortedConnections() 60 i := len(net_cons) 61 for _, v := range tmp { 62 i-- 63 v.Conn.GetStats(&net_cons[i]) 64 net_cons[i].HasImmunity = v.MinutesOnline < network.OnlineImmunityMinutes 65 } 66 67 bx, er := json.Marshal(net_cons) 68 if er == nil { 69 w.Header()["Content-Type"] = []string{"application/json"} 70 w.Write(bx) 71 } else { 72 println(er.Error()) 73 } 74 75 } 76 77 func json_peerst(w http.ResponseWriter, r *http.Request) { 78 if !ipchecker(r) { 79 return 80 } 81 82 if len(r.Form["id"]) == 0 { 83 return 84 } 85 86 conid, e := strconv.ParseUint(r.Form["id"][0], 10, 32) 87 if e != nil { 88 return 89 } 90 91 var res *network.ConnInfo 92 93 network.Mutex_net.Lock() 94 for _, v := range network.OpenCons { 95 if uint32(conid) == v.ConnID { 96 res = new(network.ConnInfo) 97 v.GetStats(res) 98 break 99 } 100 } 101 network.Mutex_net.Unlock() 102 103 if res != nil { 104 bx, er := json.Marshal(&res) 105 if er == nil { 106 w.Header()["Content-Type"] = []string{"application/json"} 107 w.Write(bx) 108 } else { 109 println(er.Error()) 110 } 111 } 112 } 113 114 func json_bwidth(w http.ResponseWriter, r *http.Request) { 115 if !ipchecker(r) { 116 return 117 } 118 119 type one_ext_ip struct { 120 Ip string 121 Count, Timestamp uint 122 } 123 124 var out struct { 125 Open_conns_total int 126 Open_conns_out uint32 127 Open_conns_in uint32 128 Dl_speed_now uint64 129 Dl_speed_max uint64 130 Dl_total uint64 131 Ul_speed_now uint64 132 Ul_speed_max uint64 133 Ul_total uint64 134 ExternalIP []one_ext_ip 135 GetMPInProgress bool 136 GetMPConnID int 137 } 138 139 common.LockBw() 140 common.TickRecv() 141 common.TickSent() 142 out.Dl_speed_now = common.GetAvgBW(common.DlBytesPrevSec[:], common.DlBytesPrevSecIdx, 5) 143 out.Dl_speed_max = common.DownloadLimit() 144 out.Dl_total = common.DlBytesTotal 145 out.Ul_speed_now = common.GetAvgBW(common.UlBytesPrevSec[:], common.UlBytesPrevSecIdx, 5) 146 out.Ul_speed_max = common.UploadLimit() 147 out.Ul_total = common.UlBytesTotal 148 common.UnlockBw() 149 150 network.Mutex_net.Lock() 151 out.Open_conns_total = len(network.OpenCons) 152 out.Open_conns_out = network.OutConsActive 153 out.Open_conns_in = network.InConsActive 154 network.Mutex_net.Unlock() 155 156 arr := network.GetExternalIPs() 157 for _, rec := range arr { 158 out.ExternalIP = append(out.ExternalIP, one_ext_ip{ 159 Ip: fmt.Sprintf("%d.%d.%d.%d", byte(rec.IP>>24), byte(rec.IP>>16), byte(rec.IP>>8), byte(rec.IP)), 160 Count: rec.Cnt, Timestamp: rec.Tim}) 161 } 162 163 out.GetMPInProgress = len(network.GetMPInProgressTicket) != 0 164 out.GetMPConnID = network.GetMPInProgressConnID.Get() 165 166 bx, er := json.Marshal(out) 167 if er == nil { 168 w.Header()["Content-Type"] = []string{"application/json"} 169 w.Write(bx) 170 } else { 171 println(er.Error()) 172 } 173 } 174 175 func json_bwchar(w http.ResponseWriter, r *http.Request) { 176 if !ipchecker(r) { 177 return 178 } 179 180 var cnt uint64 181 182 if len(r.Form["seconds"]) > 0 { 183 cnt, _ = strconv.ParseUint(r.Form["seconds"][0], 10, 32) 184 } 185 if cnt < 1 { 186 cnt = 1 187 } else if cnt > 300 { 188 cnt = 300 189 } 190 191 var out struct { 192 DL [200]uint64 // max 200 records (from 200 seconds to ~16.7 hours) 193 UL [200]uint64 194 MaxDL, MaxUL uint64 195 } 196 197 common.LockBw() 198 common.TickRecv() 199 common.TickSent() 200 201 idx := uint16(common.DlBytesPrevSecIdx) 202 for i := range out.DL { 203 var sum uint64 204 for c := 0; c < int(cnt); c++ { 205 idx-- 206 sum += common.DlBytesPrevSec[idx] 207 if common.DlBytesPrevSec[idx] > out.MaxDL { 208 out.MaxDL = common.DlBytesPrevSec[idx] 209 } 210 } 211 out.DL[i] = sum / cnt 212 } 213 214 idx = uint16(common.UlBytesPrevSecIdx) 215 for i := range out.UL { 216 var sum uint64 217 for c := 0; c < int(cnt); c++ { 218 idx-- 219 sum += common.UlBytesPrevSec[idx] 220 if common.UlBytesPrevSec[idx] > out.MaxUL { 221 out.MaxUL = common.UlBytesPrevSec[idx] 222 } 223 } 224 out.UL[i] = sum / cnt 225 } 226 227 common.UnlockBw() 228 229 bx, er := json.Marshal(out) 230 if er == nil { 231 w.Header()["Content-Type"] = []string{"application/json"} 232 w.Write(bx) 233 } else { 234 println(er.Error()) 235 } 236 }