mirror of https://github.com/nmasse-itix/wimip.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
837 B
42 lines
837 B
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"log"
|
|
"net"
|
|
"net/http"
|
|
|
|
"github.com/chrismarget/arp"
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
type IPInfo struct {
|
|
IP string `json:"ip"`
|
|
MacAddress string `json:"mac,omitempty"`
|
|
}
|
|
|
|
func main() {
|
|
log.SetFlags(log.Flags() &^ (log.Ldate | log.Ltime))
|
|
|
|
viper.SetDefault("ListenAddr", ":8080")
|
|
viper.SetEnvPrefix("WIMIP")
|
|
viper.AutomaticEnv()
|
|
|
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
var info IPInfo
|
|
ip, _, err := net.SplitHostPort(r.RemoteAddr)
|
|
if err != nil {
|
|
w.WriteHeader(500)
|
|
}
|
|
info.IP = ip
|
|
|
|
arp.CacheUpdate()
|
|
info.MacAddress = arp.Search(ip)
|
|
|
|
out, _ := json.MarshalIndent(info, "", " ")
|
|
w.Write(out)
|
|
})
|
|
|
|
log.Printf("Listening on %s...", viper.GetString("ListenAddr"))
|
|
log.Fatal(http.ListenAndServe(viper.GetString("ListenAddr"), nil))
|
|
}
|
|
|