various performance improvements and bug fixes
This commit is contained in:
1 parent
4506140223
commit
5a35bf44fb
2 files changed
+381
-186
No files matched your search
+363
-168
@@ -1,14 +1,16 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"container/list"
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"flag"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"net"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/oschwald/geoip2-golang"
|
||||
@@ -27,7 +29,17 @@ var (
|
||||
ttl = flag.Int("t", 300, "Cache TTL in seconds")
|
||||
maxcache = flag.Int("m", 2048, "Max number of entries in cache")
|
||||
|
||||
cache = make([]Cache, 0)
|
||||
// Global database connections (opened once at startup)
|
||||
geoCityDB *geoip2.Reader
|
||||
geoAsnDB *geoip2.Reader
|
||||
|
||||
// Cache with map for O(1) lookups and mutex for concurrent access
|
||||
cache = make(map[string]*CacheEntry)
|
||||
cacheList = list.New() // LRU eviction list
|
||||
cacheMutex sync.RWMutex
|
||||
|
||||
// Template parsed once at startup
|
||||
tmpl *template.Template
|
||||
|
||||
cli = []string{
|
||||
"curl",
|
||||
@@ -50,13 +62,13 @@ var (
|
||||
)
|
||||
|
||||
type Ssl struct {
|
||||
San []string
|
||||
Ocsp []string
|
||||
Subject string
|
||||
Issuer string
|
||||
Alpn string
|
||||
Not_before string
|
||||
Not_after string
|
||||
San []string
|
||||
Ocsp []string
|
||||
Subject string
|
||||
Issuer string
|
||||
Alpn string
|
||||
NotBefore string
|
||||
NotAfter string
|
||||
}
|
||||
|
||||
type Geo struct {
|
||||
@@ -75,31 +87,32 @@ type Asn struct {
|
||||
}
|
||||
|
||||
type Info struct {
|
||||
Ip string
|
||||
Hostname string
|
||||
Query bool
|
||||
Port string
|
||||
User_agent string
|
||||
Mime_type string
|
||||
Language string
|
||||
Content_type string
|
||||
Encoding string
|
||||
Method string
|
||||
Cache_control string
|
||||
Referer string
|
||||
X_forwarded_for string
|
||||
X_forwarded_proto string
|
||||
Cached bool
|
||||
Ttl int
|
||||
Geo Geo
|
||||
Asn Asn
|
||||
Ssl Ssl
|
||||
Ip string
|
||||
Hostname string
|
||||
Query bool
|
||||
Port string
|
||||
UserAgent string
|
||||
MimeType string
|
||||
Language string
|
||||
ContentType string
|
||||
Encoding string
|
||||
Method string
|
||||
CacheControl string
|
||||
Referer string
|
||||
XForwardedFor string
|
||||
XForwardedProto string
|
||||
Cached bool
|
||||
Ttl int
|
||||
Geo Geo
|
||||
Asn Asn
|
||||
Ssl Ssl
|
||||
}
|
||||
|
||||
type Cache struct {
|
||||
Query string
|
||||
Info Info
|
||||
Time time.Time
|
||||
type CacheEntry struct {
|
||||
Query string
|
||||
Info Info
|
||||
Time time.Time
|
||||
Element *list.Element // Pointer to element in LRU list for O(1) removal
|
||||
}
|
||||
|
||||
type Tmpl struct {
|
||||
@@ -114,44 +127,38 @@ type Tmpl struct {
|
||||
func getElem(info Info, elem string) string {
|
||||
elem = strings.ToLower(elem)
|
||||
elem = strings.ReplaceAll(elem, "-", "_")
|
||||
|
||||
// Use direct field access instead of large switch statement
|
||||
switch elem {
|
||||
case "hostname":
|
||||
return info.Hostname
|
||||
case "host":
|
||||
case "hostname", "host":
|
||||
return info.Hostname
|
||||
case "ip":
|
||||
return info.Ip
|
||||
case "port":
|
||||
return info.Port
|
||||
case "user_agent":
|
||||
return info.User_agent
|
||||
case "ua":
|
||||
return info.User_agent
|
||||
case "useragent":
|
||||
return info.User_agent
|
||||
case "user_agent", "ua", "useragent":
|
||||
return info.UserAgent
|
||||
case "mime_type":
|
||||
return info.Mime_type
|
||||
return info.MimeType
|
||||
case "language":
|
||||
return info.Language
|
||||
case "content_type":
|
||||
return info.Content_type
|
||||
return info.ContentType
|
||||
case "encoding":
|
||||
return info.Encoding
|
||||
case "method":
|
||||
return info.Method
|
||||
case "cache_control":
|
||||
return info.Cache_control
|
||||
return info.CacheControl
|
||||
case "referer":
|
||||
return info.Referer
|
||||
case "x_forwarded_for":
|
||||
return info.X_forwarded_for
|
||||
return info.XForwardedFor
|
||||
case "x_forwarded_proto":
|
||||
return info.X_forwarded_proto
|
||||
return info.XForwardedProto
|
||||
case "country":
|
||||
return info.Geo.Country
|
||||
case "ccode":
|
||||
return info.Geo.Ccode
|
||||
case "cc":
|
||||
case "ccode", "cc":
|
||||
return info.Geo.Ccode
|
||||
case "city":
|
||||
return info.Geo.City
|
||||
@@ -165,40 +172,22 @@ func getElem(info Info, elem string) string {
|
||||
return info.Geo.Timezone
|
||||
case "asn":
|
||||
return fmt.Sprintf("%d", info.Asn.Asn)
|
||||
case "org":
|
||||
case "org", "organisation", "isp":
|
||||
return info.Asn.Org
|
||||
case "organisation":
|
||||
return info.Asn.Org
|
||||
case "isp":
|
||||
return info.Asn.Org
|
||||
case "ssl_san":
|
||||
case "ssl_san", "san":
|
||||
return strings.Join(info.Ssl.San, ", ")
|
||||
case "ssl_ocsp":
|
||||
case "ssl_ocsp", "ocsp":
|
||||
return strings.Join(info.Ssl.Ocsp, ", ")
|
||||
case "ssl_subject":
|
||||
case "ssl_subject", "subject":
|
||||
return info.Ssl.Subject
|
||||
case "ssl_issuer":
|
||||
case "ssl_issuer", "issuer":
|
||||
return info.Ssl.Issuer
|
||||
case "ssl_alpn":
|
||||
case "ssl_alpn", "alpn":
|
||||
return info.Ssl.Alpn
|
||||
case "ssl_not_after":
|
||||
return info.Ssl.Not_after
|
||||
case "ssl_not_before":
|
||||
return info.Ssl.Not_before
|
||||
case "san":
|
||||
return strings.Join(info.Ssl.San, ", ")
|
||||
case "ocsp":
|
||||
return strings.Join(info.Ssl.Ocsp, ", ")
|
||||
case "subject":
|
||||
return info.Ssl.Subject
|
||||
case "issuer":
|
||||
return info.Ssl.Issuer
|
||||
case "alpn":
|
||||
return info.Ssl.Alpn
|
||||
case "not_after":
|
||||
return info.Ssl.Not_after
|
||||
case "not_before":
|
||||
return info.Ssl.Not_before
|
||||
case "ssl_not_after", "not_after":
|
||||
return info.Ssl.NotAfter
|
||||
case "ssl_not_before", "not_before":
|
||||
return info.Ssl.NotBefore
|
||||
}
|
||||
return ""
|
||||
}
|
||||
@@ -207,12 +196,27 @@ func writeSsl(w http.ResponseWriter, ssl Ssl) {
|
||||
if ssl.San == nil {
|
||||
return
|
||||
}
|
||||
v := reflect.ValueOf(ssl)
|
||||
t := reflect.TypeOf(ssl)
|
||||
for i := 0; i < t.NumField(); i++ {
|
||||
if v.Field(i).Interface() != "" {
|
||||
fmt.Fprintf(w, "%s: %v\n", strings.ToLower(t.Field(i).Name), v.Field(i).Interface())
|
||||
}
|
||||
// Direct field access instead of reflection
|
||||
if len(ssl.San) > 0 {
|
||||
fmt.Fprintf(w, "san: %v\n", ssl.San)
|
||||
}
|
||||
if len(ssl.Ocsp) > 0 {
|
||||
fmt.Fprintf(w, "ocsp: %v\n", ssl.Ocsp)
|
||||
}
|
||||
if ssl.Subject != "" {
|
||||
fmt.Fprintf(w, "subject: %s\n", ssl.Subject)
|
||||
}
|
||||
if ssl.Issuer != "" {
|
||||
fmt.Fprintf(w, "issuer: %s\n", ssl.Issuer)
|
||||
}
|
||||
if ssl.Alpn != "" {
|
||||
fmt.Fprintf(w, "alpn: %s\n", ssl.Alpn)
|
||||
}
|
||||
if ssl.NotBefore != "" {
|
||||
fmt.Fprintf(w, "notbefore: %s\n", ssl.NotBefore)
|
||||
}
|
||||
if ssl.NotAfter != "" {
|
||||
fmt.Fprintf(w, "notafter: %s\n", ssl.NotAfter)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -220,12 +224,9 @@ func writeAsn(w http.ResponseWriter, asn Asn) {
|
||||
if asn.Asn == 0 {
|
||||
return
|
||||
}
|
||||
v := reflect.ValueOf(asn)
|
||||
t := reflect.TypeOf(asn)
|
||||
for i := 0; i < t.NumField(); i++ {
|
||||
if v.Field(i).Interface() != "" {
|
||||
fmt.Fprintf(w, "%s: %v\n", strings.ToLower(t.Field(i).Name), v.Field(i).Interface())
|
||||
}
|
||||
fmt.Fprintf(w, "asn: %d\n", asn.Asn)
|
||||
if asn.Org != "" {
|
||||
fmt.Fprintf(w, "org: %s\n", asn.Org)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -233,32 +234,76 @@ func writeGeo(w http.ResponseWriter, geo Geo) {
|
||||
if geo.Country == "" {
|
||||
return
|
||||
}
|
||||
v := reflect.ValueOf(geo)
|
||||
t := reflect.TypeOf(geo)
|
||||
for i := 0; i < t.NumField(); i++ {
|
||||
if v.Field(i).Interface() != "" {
|
||||
fmt.Fprintf(w, "%s: %v\n", strings.ToLower(t.Field(i).Name), v.Field(i).Interface())
|
||||
}
|
||||
fmt.Fprintf(w, "country: %s\n", geo.Country)
|
||||
if geo.Ccode != "" {
|
||||
fmt.Fprintf(w, "ccode: %s\n", geo.Ccode)
|
||||
}
|
||||
if geo.City != "" {
|
||||
fmt.Fprintf(w, "city: %s\n", geo.City)
|
||||
}
|
||||
if geo.Latitude != 0 {
|
||||
fmt.Fprintf(w, "latitude: %f\n", geo.Latitude)
|
||||
}
|
||||
if geo.Longitude != 0 {
|
||||
fmt.Fprintf(w, "longitude: %f\n", geo.Longitude)
|
||||
}
|
||||
if geo.Postal != "" {
|
||||
fmt.Fprintf(w, "postal: %s\n", geo.Postal)
|
||||
}
|
||||
if geo.Timezone != "" {
|
||||
fmt.Fprintf(w, "timezone: %s\n", geo.Timezone)
|
||||
}
|
||||
}
|
||||
|
||||
func writeInfo(w http.ResponseWriter, info Info) {
|
||||
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
v := reflect.ValueOf(info)
|
||||
t := reflect.TypeOf(info)
|
||||
for i := 0; i < t.NumField(); i++ {
|
||||
if t.Field(i).Name == "Query" || t.Field(i).Name == "Cached" {
|
||||
continue
|
||||
} else if t.Field(i).Name == "Geo" {
|
||||
writeGeo(w, v.Field(i).Interface().(Geo))
|
||||
} else if t.Field(i).Name == "Asn" {
|
||||
writeAsn(w, v.Field(i).Interface().(Asn))
|
||||
} else if t.Field(i).Name == "Ssl" {
|
||||
writeSsl(w, v.Field(i).Interface().(Ssl))
|
||||
} else if v.Field(i).Interface() != "" {
|
||||
fmt.Fprintf(w, "%s: %v\n", strings.ToLower(t.Field(i).Name), v.Field(i).Interface())
|
||||
}
|
||||
// Direct field access instead of reflection for better performance
|
||||
if info.Ip != "" {
|
||||
fmt.Fprintf(w, "ip: %s\n", info.Ip)
|
||||
}
|
||||
if info.Hostname != "" {
|
||||
fmt.Fprintf(w, "hostname: %s\n", info.Hostname)
|
||||
}
|
||||
if info.Port != "" {
|
||||
fmt.Fprintf(w, "port: %s\n", info.Port)
|
||||
}
|
||||
if info.UserAgent != "" {
|
||||
fmt.Fprintf(w, "useragent: %s\n", info.UserAgent)
|
||||
}
|
||||
if info.MimeType != "" {
|
||||
fmt.Fprintf(w, "mimetype: %s\n", info.MimeType)
|
||||
}
|
||||
if info.Language != "" {
|
||||
fmt.Fprintf(w, "language: %s\n", info.Language)
|
||||
}
|
||||
if info.ContentType != "" {
|
||||
fmt.Fprintf(w, "contenttype: %s\n", info.ContentType)
|
||||
}
|
||||
if info.Encoding != "" {
|
||||
fmt.Fprintf(w, "encoding: %s\n", info.Encoding)
|
||||
}
|
||||
if info.Method != "" {
|
||||
fmt.Fprintf(w, "method: %s\n", info.Method)
|
||||
}
|
||||
if info.CacheControl != "" {
|
||||
fmt.Fprintf(w, "cachecontrol: %s\n", info.CacheControl)
|
||||
}
|
||||
if info.Referer != "" {
|
||||
fmt.Fprintf(w, "referer: %s\n", info.Referer)
|
||||
}
|
||||
if info.XForwardedFor != "" {
|
||||
fmt.Fprintf(w, "xforwardedfor: %s\n", info.XForwardedFor)
|
||||
}
|
||||
if info.XForwardedProto != "" {
|
||||
fmt.Fprintf(w, "xforwardedproto: %s\n", info.XForwardedProto)
|
||||
}
|
||||
if info.Ttl > 0 {
|
||||
fmt.Fprintf(w, "ttl: %d\n", info.Ttl)
|
||||
}
|
||||
writeGeo(w, info.Geo)
|
||||
writeAsn(w, info.Asn)
|
||||
writeSsl(w, info.Ssl)
|
||||
}
|
||||
|
||||
func reqtype(ip string) string {
|
||||
@@ -283,7 +328,17 @@ func reqtype(ip string) string {
|
||||
func getSslInfo(domain string, info Info) Info {
|
||||
lookup, err := net.LookupIP(domain)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
if *verbose {
|
||||
fmt.Printf("DNS lookup error for %s: %v\n", domain, err)
|
||||
}
|
||||
return info
|
||||
}
|
||||
|
||||
// Check if lookup returned any results
|
||||
if len(lookup) == 0 {
|
||||
if *verbose {
|
||||
fmt.Printf("DNS lookup returned no results for %s\n", domain)
|
||||
}
|
||||
return info
|
||||
}
|
||||
info.Ip = lookup[0].String()
|
||||
@@ -294,35 +349,43 @@ func getSslInfo(domain string, info Info) Info {
|
||||
}, "tcp", domain+":443", &tls.Config{InsecureSkipVerify: true})
|
||||
if err != nil {
|
||||
if *verbose {
|
||||
fmt.Println(err)
|
||||
fmt.Printf("TLS connection error for %s: %v\n", domain, err)
|
||||
}
|
||||
return info
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
ssl.San = conn.ConnectionState().PeerCertificates[0].DNSNames
|
||||
ssl.Subject = conn.ConnectionState().PeerCertificates[0].Subject.String()
|
||||
ssl.Issuer = conn.ConnectionState().PeerCertificates[0].Issuer.String()
|
||||
// Check if peer certificates are available
|
||||
certs := conn.ConnectionState().PeerCertificates
|
||||
if len(certs) == 0 {
|
||||
if *verbose {
|
||||
fmt.Printf("No peer certificates received for %s\n", domain)
|
||||
}
|
||||
return info
|
||||
}
|
||||
|
||||
ssl.San = certs[0].DNSNames
|
||||
ssl.Subject = certs[0].Subject.String()
|
||||
ssl.Issuer = certs[0].Issuer.String()
|
||||
ssl.Alpn = conn.ConnectionState().NegotiatedProtocol
|
||||
ssl.Ocsp = conn.ConnectionState().PeerCertificates[0].OCSPServer
|
||||
ssl.Not_after = conn.ConnectionState().PeerCertificates[0].NotAfter.String()
|
||||
ssl.Not_before = conn.ConnectionState().PeerCertificates[0].NotBefore.String()
|
||||
ssl.Ocsp = certs[0].OCSPServer
|
||||
ssl.NotAfter = certs[0].NotAfter.String()
|
||||
ssl.NotBefore = certs[0].NotBefore.String()
|
||||
info.Ssl = ssl
|
||||
return info
|
||||
}
|
||||
|
||||
func getAsn(ip string) Asn {
|
||||
asn := Asn{}
|
||||
ipdb, err := geoip2.Open(*geoasn)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return Asn{}
|
||||
if geoAsnDB == nil {
|
||||
return asn
|
||||
}
|
||||
defer ipdb.Close()
|
||||
|
||||
record, err := ipdb.ASN(net.ParseIP(ip))
|
||||
record, err := geoAsnDB.ASN(net.ParseIP(ip))
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
if *verbose {
|
||||
fmt.Printf("ASN lookup error for %s: %v\n", ip, err)
|
||||
}
|
||||
return Asn{}
|
||||
}
|
||||
|
||||
@@ -333,16 +396,15 @@ func getAsn(ip string) Asn {
|
||||
|
||||
func getGeo(ip string) Geo {
|
||||
geo := Geo{}
|
||||
ipdb, err := geoip2.Open(*geocity)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return Geo{}
|
||||
if geoCityDB == nil {
|
||||
return geo
|
||||
}
|
||||
defer ipdb.Close()
|
||||
|
||||
record, err := ipdb.City(net.ParseIP(ip))
|
||||
record, err := geoCityDB.City(net.ParseIP(ip))
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
if *verbose {
|
||||
fmt.Printf("GeoIP lookup error for %s: %v\n", ip, err)
|
||||
}
|
||||
return Geo{}
|
||||
}
|
||||
|
||||
@@ -369,7 +431,9 @@ func getIP(info Info, r *http.Request) (Info, error) {
|
||||
} else {
|
||||
info.Ip, info.Port, err = net.SplitHostPort(r.RemoteAddr)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
if *verbose {
|
||||
fmt.Printf("Error parsing remote address %s: %v\n", r.RemoteAddr, err)
|
||||
}
|
||||
return info, err
|
||||
}
|
||||
}
|
||||
@@ -377,47 +441,108 @@ func getIP(info Info, r *http.Request) (Info, error) {
|
||||
}
|
||||
|
||||
func getIpInfo(info Info, r *http.Request) (Info, error) {
|
||||
hosts, err := net.LookupAddr(info.Ip)
|
||||
// Create a context with timeout for DNS lookups
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 2*time.Second)
|
||||
defer cancel()
|
||||
|
||||
// Use a custom resolver with timeout
|
||||
resolver := &net.Resolver{}
|
||||
hosts, err := resolver.LookupAddr(ctx, info.Ip)
|
||||
if err != nil {
|
||||
// If lookup fails or times out, use the IP as hostname
|
||||
if *verbose {
|
||||
fmt.Printf("Reverse DNS lookup error for %s: %v\n", info.Ip, err)
|
||||
}
|
||||
hosts = []string{info.Ip}
|
||||
}
|
||||
info.Hostname = hosts[0]
|
||||
|
||||
if len(hosts) > 0 {
|
||||
info.Hostname = hosts[0]
|
||||
} else {
|
||||
info.Hostname = info.Ip
|
||||
}
|
||||
|
||||
if !info.Query {
|
||||
info.User_agent = r.UserAgent()
|
||||
info.Mime_type = r.Header.Get("Accept")
|
||||
info.UserAgent = r.UserAgent()
|
||||
info.MimeType = r.Header.Get("Accept")
|
||||
info.Language = r.Header.Get("Accept-Language")
|
||||
info.Content_type = r.Header.Get("Content-Type")
|
||||
info.ContentType = r.Header.Get("Content-Type")
|
||||
info.Encoding = r.Header.Get("Accept-Encoding")
|
||||
info.Method = r.Method
|
||||
info.Cache_control = r.Header.Get("Cache-Control")
|
||||
info.CacheControl = r.Header.Get("Cache-Control")
|
||||
info.Referer = r.Header.Get("Referer")
|
||||
info.X_forwarded_for = r.Header.Get("X-Forwarded-For")
|
||||
info.X_forwarded_proto = r.Header.Get("X-Forwarded-Proto")
|
||||
info.XForwardedFor = r.Header.Get("X-Forwarded-For")
|
||||
info.XForwardedProto = r.Header.Get("X-Forwarded-Proto")
|
||||
}
|
||||
return info, nil
|
||||
}
|
||||
|
||||
func getCache(query string, info Info) Info {
|
||||
for i := 0; i < len(cache); i++ {
|
||||
if cache[i].Query == query {
|
||||
if time.Since(cache[i].Time) < time.Duration(*ttl)*time.Second {
|
||||
cache[i].Info.Ttl = int(time.Duration(*ttl)*time.Second-time.Since(cache[i].Time)) / 1000000000
|
||||
return cache[i].Info
|
||||
} else {
|
||||
cache = append(cache[:i], cache[i+1:]...)
|
||||
}
|
||||
}
|
||||
cacheMutex.RLock()
|
||||
entry, exists := cache[query]
|
||||
cacheMutex.RUnlock()
|
||||
|
||||
if !exists {
|
||||
return info
|
||||
}
|
||||
|
||||
elapsed := time.Since(entry.Time)
|
||||
if elapsed < time.Duration(*ttl)*time.Second {
|
||||
entry.Info.Ttl = int((time.Duration(*ttl)*time.Second - elapsed).Seconds())
|
||||
entry.Info.Cached = true
|
||||
|
||||
// Move to front of LRU list (most recently used)
|
||||
cacheMutex.Lock()
|
||||
cacheList.MoveToFront(entry.Element)
|
||||
cacheMutex.Unlock()
|
||||
|
||||
return entry.Info
|
||||
}
|
||||
|
||||
// Entry expired, remove it
|
||||
cacheMutex.Lock()
|
||||
if entry.Element != nil {
|
||||
cacheList.Remove(entry.Element)
|
||||
}
|
||||
delete(cache, query)
|
||||
cacheMutex.Unlock()
|
||||
return info
|
||||
}
|
||||
|
||||
func putCache(query string, info Info) {
|
||||
info.Cached = true
|
||||
info.Ttl = *ttl
|
||||
cache = append(cache, Cache{Info: info, Query: query, Time: time.Now()})
|
||||
if len(cache) > *maxcache {
|
||||
cache = cache[1:]
|
||||
|
||||
cacheMutex.Lock()
|
||||
defer cacheMutex.Unlock()
|
||||
|
||||
// Check if entry already exists (update case)
|
||||
if existing, exists := cache[query]; exists {
|
||||
existing.Info = info
|
||||
existing.Time = time.Now()
|
||||
cacheList.MoveToFront(existing.Element)
|
||||
return
|
||||
}
|
||||
|
||||
// LRU eviction if cache is full
|
||||
if len(cache) >= *maxcache {
|
||||
// Remove least recently used (back of list)
|
||||
oldest := cacheList.Back()
|
||||
if oldest != nil {
|
||||
oldEntry := oldest.Value.(*CacheEntry)
|
||||
delete(cache, oldEntry.Query)
|
||||
cacheList.Remove(oldest)
|
||||
}
|
||||
}
|
||||
|
||||
// Add new entry
|
||||
entry := &CacheEntry{
|
||||
Query: query,
|
||||
Info: info,
|
||||
Time: time.Now(),
|
||||
}
|
||||
entry.Element = cacheList.PushFront(entry)
|
||||
cache[query] = entry
|
||||
}
|
||||
|
||||
func handler(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -445,10 +570,13 @@ func handler(w http.ResponseWriter, r *http.Request) {
|
||||
info, err = getIP(info, r)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
fmt.Println(err)
|
||||
if *verbose {
|
||||
fmt.Printf("Error getting IP: %v\n", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
if isCli {
|
||||
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(info.Ip + "\n"))
|
||||
return
|
||||
@@ -468,7 +596,10 @@ func handler(w http.ResponseWriter, r *http.Request) {
|
||||
info, err = getIpInfo(info, r)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
fmt.Println(err)
|
||||
if *verbose {
|
||||
fmt.Printf("Error getting IP info: %v\n", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
@@ -480,6 +611,12 @@ func handler(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
args := strings.Split(r.URL.Path, "/")
|
||||
if len(args) > 1 && args[1] != "" {
|
||||
// Validate that args[1] is not excessively long (prevent DoS)
|
||||
if len(args[1]) > 256 {
|
||||
http.Error(w, "Request URI too long", http.StatusRequestURITooLong)
|
||||
return
|
||||
}
|
||||
|
||||
arg = "/" + args[1]
|
||||
rtype := reqtype(args[1])
|
||||
if rtype == "" {
|
||||
@@ -497,7 +634,9 @@ func handler(w http.ResponseWriter, r *http.Request) {
|
||||
info, err = getIP(info, r)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
fmt.Println(err)
|
||||
if *verbose {
|
||||
fmt.Printf("Error getting IP: %v\n", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
info = getCache(info.Ip, info)
|
||||
@@ -531,32 +670,37 @@ func handler(w http.ResponseWriter, r *http.Request) {
|
||||
info, err = getIpInfo(info, r)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
fmt.Println(err)
|
||||
if *verbose {
|
||||
fmt.Printf("Error getting IP info: %v\n", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if rtype == "info" || (len(args) > 2 && args[2] != "") {
|
||||
if args[len(args)-1] == "all" {
|
||||
// Safely access the last argument
|
||||
lastArg := args[len(args)-1]
|
||||
if lastArg == "all" {
|
||||
writeInfo(w, info)
|
||||
return
|
||||
}
|
||||
if args[len(args)-1] == "geo" {
|
||||
if lastArg == "geo" {
|
||||
writeGeo(w, info.Geo)
|
||||
return
|
||||
}
|
||||
if args[len(args)-1] == "as" {
|
||||
if lastArg == "as" {
|
||||
writeAsn(w, info.Asn)
|
||||
return
|
||||
}
|
||||
if args[len(args)-1] == "ssl" {
|
||||
if lastArg == "ssl" {
|
||||
writeSsl(w, info.Ssl)
|
||||
return
|
||||
}
|
||||
res := getElem(info, args[len(args)-1])
|
||||
res := getElem(info, lastArg)
|
||||
if res == "" {
|
||||
http.Error(w, "Not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
fmt.Fprintf(w, "%s\n", res)
|
||||
return
|
||||
@@ -568,7 +712,7 @@ func handler(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
tmpl := Tmpl{
|
||||
tmplData := Tmpl{
|
||||
Title: *name,
|
||||
Domain: *domain,
|
||||
Short: *shortdomain,
|
||||
@@ -576,20 +720,71 @@ func handler(w http.ResponseWriter, r *http.Request) {
|
||||
Argument: arg,
|
||||
Info: info,
|
||||
}
|
||||
t, err := template.ParseFiles("template.html")
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
fmt.Println(err)
|
||||
|
||||
if tmpl == nil {
|
||||
http.Error(w, "Template not initialized", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
err = t.Execute(w, tmpl)
|
||||
|
||||
// Set Content-Type header for HTML response
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
err = tmpl.Execute(w, tmplData)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
if *verbose {
|
||||
fmt.Printf("Template execution error: %v\n", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func initialize() error {
|
||||
var err error
|
||||
|
||||
// Open GeoIP City database if path is provided
|
||||
if *geocity != "" {
|
||||
geoCityDB, err = geoip2.Open(*geocity)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to open GeoIP City database: %w", err)
|
||||
}
|
||||
fmt.Println("GeoIP City database loaded:", *geocity)
|
||||
}
|
||||
|
||||
// Open GeoIP ASN database if path is provided
|
||||
if *geoasn != "" {
|
||||
geoAsnDB, err = geoip2.Open(*geoasn)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to open GeoIP ASN database: %w", err)
|
||||
}
|
||||
fmt.Println("GeoIP ASN database loaded:", *geoasn)
|
||||
}
|
||||
|
||||
// Parse template once at startup
|
||||
tmpl, err = template.ParseFiles("template.html")
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to parse template: %w", err)
|
||||
}
|
||||
fmt.Println("Template loaded: template.html")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
// Initialize databases and template
|
||||
if err := initialize(); err != nil {
|
||||
fmt.Println("Initialization error:", err)
|
||||
return
|
||||
}
|
||||
defer func() {
|
||||
if geoCityDB != nil {
|
||||
geoCityDB.Close()
|
||||
}
|
||||
if geoAsnDB != nil {
|
||||
geoAsnDB.Close()
|
||||
}
|
||||
}()
|
||||
|
||||
fmt.Println("Listening on port", *port)
|
||||
http.HandleFunc("/", handler)
|
||||
err := http.ListenAndServe(fmt.Sprintf("%s:%d", *bind, *port), nil)
|
||||
|
||||
+18
-18
@@ -260,16 +260,16 @@
|
||||
<div class="col-sm-9">{{ .Info.Port }}</div>
|
||||
</li>
|
||||
{{ end }}
|
||||
{{ if .Info.User_agent }}
|
||||
{{ if .Info.UserAgent }}
|
||||
<li class="list-group-item">
|
||||
<div class="col-sm-3">User-agent</div>
|
||||
<div class="col-sm-9">{{ .Info.User_agent }}</div>
|
||||
<div class="col-sm-9">{{ .Info.UserAgent }}</div>
|
||||
</li>
|
||||
{{ end }}
|
||||
{{ if .Info.Mime_type }}
|
||||
{{ if .Info.MimeType }}
|
||||
<li class="list-group-item">
|
||||
<div class="col-sm-3">Mime-type</div>
|
||||
<div class="col-sm-9">{{ .Info.Mime_type }}</div>
|
||||
<div class="col-sm-9">{{ .Info.MimeType }}</div>
|
||||
</li>
|
||||
{{ end }}
|
||||
{{ if .Info.Language }}
|
||||
@@ -278,10 +278,10 @@
|
||||
<div class="col-sm-9">{{ .Info.Language }}</div>
|
||||
</li>
|
||||
{{ end }}
|
||||
{{ if .Info.Content_type }}
|
||||
{{ if .Info.ContentType }}
|
||||
<li class="list-group-item">
|
||||
<div class="col-sm-3">Content-type</div>
|
||||
<div class="col-sm-9">{{ .Info.Content_type }}</div>
|
||||
<div class="col-sm-9">{{ .Info.ContentType }}</div>
|
||||
</li>
|
||||
{{ end }}
|
||||
{{ if .Info.Encoding }}
|
||||
@@ -296,10 +296,10 @@
|
||||
<div class="col-sm-9">{{ .Info.Method }}</div>
|
||||
</li>
|
||||
{{ end }}
|
||||
{{ if .Info.Cache_control }}
|
||||
{{ if .Info.CacheControl }}
|
||||
<li class="list-group-item">
|
||||
<div class="col-sm-3">Cache-control</div>
|
||||
<div class="col-sm-9">{{ .Info.Cache_control }}</div>
|
||||
<div class="col-sm-9">{{ .Info.CacheControl }}</div>
|
||||
</li>
|
||||
{{ end }}
|
||||
{{ if .Info.Referer }}
|
||||
@@ -308,16 +308,16 @@
|
||||
<div class="col-sm-9">{{ .Info.Referer }}</div>
|
||||
</li>
|
||||
{{ end }}
|
||||
{{ if .Info.X_forwarded_for }}
|
||||
{{ if .Info.XForwardedFor }}
|
||||
<li class="list-group-item">
|
||||
<div class="col-sm-3">X-Forwarded-For</div>
|
||||
<div class="col-sm-9">{{ .Info.X_forwarded_for }}</div>
|
||||
<div class="col-sm-9">{{ .Info.XForwardedFor }}</div>
|
||||
</li>
|
||||
{{ end }}
|
||||
{{ if .Info.X_forwarded_proto }}
|
||||
{{ if .Info.XForwardedProto }}
|
||||
<li class="list-group-item">
|
||||
<div class="col-sm-3">X-Forwarded-Proto</div>
|
||||
<div class="col-sm-9">{{ .Info.X_forwarded_proto }}</div>
|
||||
<div class="col-sm-9">{{ .Info.XForwardedProto }}</div>
|
||||
</li>
|
||||
{{ end }}
|
||||
</ul>
|
||||
@@ -410,16 +410,16 @@
|
||||
<div class="col-sm-9">{{ .Info.Ssl.Issuer }}</div>
|
||||
</li>
|
||||
{{ end }}
|
||||
{{ if .Info.Ssl.Not_before }}
|
||||
{{ if .Info.Ssl.NotBefore }}
|
||||
<li class="list-group-item">
|
||||
<div class="col-sm-3">Not before</div>
|
||||
<div class="col-sm-9">{{ .Info.Ssl.Not_before }}</div>
|
||||
<div class="col-sm-9">{{ .Info.Ssl.NotBefore }}</div>
|
||||
</li>
|
||||
{{ end }}
|
||||
{{ if .Info.Ssl.Not_after }}
|
||||
{{ if .Info.Ssl.NotAfter }}
|
||||
<li class="list-group-item">
|
||||
<div class="col-sm-3">Not after</div>
|
||||
<div class="col-sm-9">{{ .Info.Ssl.Not_after }}</div>
|
||||
<div class="col-sm-9">{{ .Info.Ssl.NotAfter }}</div>
|
||||
</li>
|
||||
{{ end }}
|
||||
{{ if .Info.Ssl.Ocsp }}
|
||||
@@ -448,10 +448,10 @@
|
||||
<div class="col-sm-4">curl {{ .Short }}{{ .Argument }}</div>
|
||||
<div class="col-sm-8">{{ .Info.Ip }}</div>
|
||||
</li>
|
||||
{{ if .Info.User_agent }}
|
||||
{{ if .Info.UserAgent }}
|
||||
<li class="list-group-item">
|
||||
<div class="col-sm-4">curl {{ .Short }}{{ .Argument }}/user-agent</div>
|
||||
<div class="col-sm-8">{{ .Info.User_agent }}</div>
|
||||
<div class="col-sm-8">{{ .Info.UserAgent }}</div>
|
||||
</li>
|
||||
{{ end }}
|
||||
{{ if .Info.Geo.Country }}
|
||||
|
||||
Reference in new issue
Block a user