add probe folder
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
package geo
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type GeoInfo struct {
|
||||
Country string `json:"country"`
|
||||
Region string `json:"regionName"`
|
||||
City string `json:"city"`
|
||||
}
|
||||
|
||||
func GetGeoLocation() GeoInfo {
|
||||
resp, err := http.Get("http://ip-api.com/json")
|
||||
if err != nil {
|
||||
return GeoInfo{Country: "Unknown", Region: "Unknown", City: "Unknown"}
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
var info GeoInfo
|
||||
json.NewDecoder(resp.Body).Decode(&info)
|
||||
return info
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package probe
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
type PingResult struct {
|
||||
Target string
|
||||
Status string
|
||||
StatusCode int
|
||||
LatencyMs int64
|
||||
ErrorMsg string
|
||||
Timestamp string
|
||||
}
|
||||
|
||||
func HttpPing(target string) PingResult {
|
||||
start := time.Now()
|
||||
resp, err := http.Get(target)
|
||||
latency := time.Since(start).Milliseconds()
|
||||
timestamp := time.Now().UTC().Format(time.RFC3339)
|
||||
|
||||
result := PingResult{
|
||||
Target: target,
|
||||
Timestamp: timestamp,
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
result.Status = "down"
|
||||
result.LatencyMs = 0
|
||||
result.StatusCode = 0
|
||||
result.ErrorMsg = err.Error()
|
||||
} else {
|
||||
defer resp.Body.Close()
|
||||
result.Status = "up"
|
||||
result.LatencyMs = latency
|
||||
result.StatusCode = resp.StatusCode
|
||||
result.ErrorMsg = ""
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package proof
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
|
||||
"git.cryptolab.re/foudre/whitepaper_obsero/pkg/geo"
|
||||
"git.cryptolab.re/foudre/whitepaper_obsero/pkg/probe"
|
||||
)
|
||||
|
||||
type Observation struct {
|
||||
Timestamp string `json:"timestamp"`
|
||||
Target string `json:"target"`
|
||||
Status string `json:"status"`
|
||||
HTTPStatus int `json:"http_status_code"`
|
||||
LatencyMs int64 `json:"latency_ms"`
|
||||
Error string `json:"error_message"`
|
||||
Observer ObserverInfo `json:"observer"`
|
||||
Version string `json:"version"`
|
||||
}
|
||||
|
||||
type ObserverInfo struct {
|
||||
Address string `json:"address"`
|
||||
Country string `json:"country"`
|
||||
Region string `json:"region"`
|
||||
City string `json:"city"`
|
||||
ProbeVersion string `json:"probe_version"`
|
||||
}
|
||||
|
||||
func BuildProof(result probe.PingResult, loc geo.GeoInfo) Observation {
|
||||
return Observation{
|
||||
Timestamp: result.Timestamp,
|
||||
Target: result.Target,
|
||||
Status: result.Status,
|
||||
HTTPStatus: result.StatusCode,
|
||||
LatencyMs: result.LatencyMs,
|
||||
Error: result.ErrorMsg,
|
||||
Version: "0.1",
|
||||
Observer: ObserverInfo{
|
||||
Address: "0xABCD...1234",
|
||||
Country: loc.Country,
|
||||
Region: loc.Region,
|
||||
City: loc.City,
|
||||
ProbeVersion: "0.1",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func SaveProof(obs Observation, filename string) {
|
||||
file, _ := json.MarshalIndent(obs, "", " ")
|
||||
_ = os.WriteFile(filename, file, 0644)
|
||||
}
|
||||
Reference in New Issue
Block a user