Skip to content

Commit

Permalink
feat: implement hotomoe-speedchecker
Browse files Browse the repository at this point in the history
  • Loading branch information
Hoto-Cocoa committed Mar 4, 2024
1 parent cdc5f40 commit cf370cf
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.go]
indent_style = tab

[*.bat]
end_of_line = crlf
charset = unset
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist
Binary file added assets/icon.ico
Binary file not shown.
33 changes: 33 additions & 0 deletions assets/syso.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"Icons": [
{
"ID": 1,
"Path": "assets/icon.ico"
}
],
"VersionInfos": [
{
"ID": 1,
"StringTables": [
{
"Language": "0409",
"Charset": "04b0",
"Strings": {
"CompanyName": "hotomoe",
"FileDescription": "hotomoe network checker",
"InternalName": "hotomoe-speedchecker",
"LegalCopyright": "\u00a9 hotomoe, All rights reserved.",
"OriginalFilename": "hotomoe-network-checker.exe",
"ProductName": "hotomoe-network-checker"
}
}
],
"Translations": [
{
"Language": "0409",
"Charset": "04b0"
}
]
}
]
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module hotomoe-speedchecker

go 1.20
95 changes: 95 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package main

import (
"bufio"
"context"
"crypto/tls"
"encoding/json"
"net"
"net/http"
"os"
"time"
)

type IfconfigInfo struct {
IP string `json:"ip"`
IPDecimal int `json:"ip_decimal"`
Country string `json:"country"`
CountryIso string `json:"country_iso"`
CountryEu bool `json:"country_eu"`
Asn string `json:"asn"`
AsnOrg string `json:"asn_org"`
Hostname string `json:"hostname"`
}

func main() {
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}

hosts := []string{"hoto.moe", "jp1.hotomoe.net", "us1.hotomoe.net", "jp1-cf.hotomoe.net", "us1-cf.hotomoe.net"}

info, err := getIfconfigInfo("")
if err == nil {
println("Client IP:", info.IP)
println("Client Country:", info.Country)
println("Client ASN:", info.Asn)
} else {
panic("Failed to get client IP")
}

println()

for _, host := range hosts {
println("Host:", host)

ip := getIpAddress(host)
println("IP:", ip)

info, err := getIfconfigInfo(ip)
if err == nil {
println("ASN:", info.Asn)
}
latency := getLatency(host)
println("Latency:", latency, "ms")

println()
}

println("Press Enter to exit")
bufio.NewReader(os.Stdin).ReadBytes('\n')
}

func getLatency(host string) int64 {
currentTime := time.Now().UnixMilli()

resp, err := http.Get("https://" + host)
if err != nil {
return -1
}
defer resp.Body.Close()

latency := time.Now().UnixMilli() - currentTime

return latency
}

func getIpAddress(dnsName string) string {
ips, err := net.DefaultResolver.LookupNetIP(context.Background(), "ip4", dnsName)
if err != nil {
return "unknown"
}
return ips[0].String()
}

func getIfconfigInfo(ip string) (IfconfigInfo, error) {
var info IfconfigInfo

resp, err := http.Get("https://ifconfig.co/json?ip=" + ip)
if err != nil {
return info, err
}
defer resp.Body.Close()

json.NewDecoder(resp.Body).Decode(&info)

return info, nil
}
11 changes: 11 additions & 0 deletions scripts/build.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
for /f "delims=" %%A in ('git rev-parse --show-toplevel') do (cd %%A)
copy /Y assets\syso.json syso.json
%USERPROFILE%\go\bin\syso.exe
del syso.json
SET GOOS=windows& go build -ldflags "-s -w" -o "dist/hnc-windows-amd64.exe"
SET GOOS=linux& go build -ldflags "-s -w" -o "dist/hnc-linux-amd64"
SET GOOS=darwin& go build -ldflags "-s -w" -o "dist/hnc-darwin-amd64"
del out.syso
upx --best --lzma "dist/hnc-windows-amd64.exe"
upx --best --lzma "dist/hnc-linux-amd64"
REM upx --best --lzma "dist/hnc-darwin-amd64"

0 comments on commit cf370cf

Please sign in to comment.