telebit/cmd/iplist/iplist.go

47 lines
803 B
Go
Raw Normal View History

2020-08-25 07:29:10 +00:00
package main
import (
"fmt"
"net"
"os"
"git.rootprojects.org/root/telebit/iplist"
)
func help() {
2020-08-25 08:37:19 +00:00
fmt.Fprintf(os.Stderr, "Usage: iplist _allowed.domain.tld 123.45.6.78\n")
fmt.Fprintf(os.Stderr, "(`dig TXT +short _allowed.domain.tld` should return a list like `v=spf1 ip4:123.45.6.78 ip4:123.45.6.1/24`\n")
2020-08-25 07:29:10 +00:00
os.Exit(1)
}
func main() {
if 3 != len(os.Args) {
help()
return
}
txtDomain := os.Args[1]
remoteIP := net.ParseIP(os.Args[2])
if nil == remoteIP {
fmt.Fprintf(os.Stderr, "bad remote IP\n")
os.Exit(1)
return
}
iplist.Init(txtDomain)
allowed, err := iplist.IsAllowed(remoteIP)
if nil != err {
fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1)
return
}
if !allowed {
fmt.Fprintf(os.Stderr, "not allowed\n")
os.Exit(1)
return
}
fmt.Println("allowed")
}