5 Commits

Author SHA1 Message Date
brent saner
9f97fcaf81 v0.2.0
ADDED:
* The ability to show both IPv4 and IPv6 addresses (if the client has
  dual-stack and either the server does as well or a separate ClientInfo
  is running on the "other" net family).
2025-12-13 04:19:05 -05:00
brent saner
eb5c44e1c3 v0.1.0
Fixes confirmed; see tag message for details.
2024-12-19 02:56:26 -05:00
brent saner
6b75e17f48 BUGFIX: Content-Type, Nil Raw Body Edge Case, Links
FIXES:
* There was an edge case where a raw body for HTML would potentially
  result in a nil byte slice exception. This has been fixed.
  (I don't even know if it was possible, I just made sure it wasn't.)
* The links browser is now explicitly returned as HTML and properly
  detected as a "browser".
* Hyperlinks for links, w3m added to Usage page.
* Content-Type is now always set correctly; there were cases where it
  was improperly returning e.g. text/plain for JSON.
2024-12-19 02:27:53 -05:00
brent saner
8a0465a0f4 update TODO 2024-12-12 04:37:55 -05:00
brent saner
c3b16f3981 update _extras; fix typo (htttp => http), and add rest of options in clientinfo.env 2024-12-12 04:23:20 -05:00
15 changed files with 314 additions and 96 deletions

20
README.adoc Normal file
View File

@@ -0,0 +1,20 @@
= ClientInfo
r00t^2 <brent.saner@gmail.com>
Last rendered {localdatetime}
:doctype: book
:docinfo: shared
:data-uri:
:imagesdir: images
:sectlinks:
:sectnums:
:sectnumlevels: 7
:toc: preamble
:toc2: left
:idprefix:
:toclevels: 7
//:toclevels: 4
:source-highlighter: rouge
:docinfo: shared
== DOCS
**TODO!**

5
TODO Normal file
View File

@@ -0,0 +1,5 @@
- suggest browser, OS alternatives (again)
- implement ipinfo.io equiv. myself?
- also link to https://www.useragentstring.com/
-- or maybe https://www.whatsmyua.info/
-- see https://www.useragentstring.com/pages/api.php

View File

@@ -20,3 +20,36 @@
# * "<Client IP>:<Client Port>" (IPv4)
# * "[<Client IP>]:<Client Port>" (IPv6)
#CINFO_URI="http://127.0.0.1:1234"
# The permissions mode for the socket file, if using UDS.
#CINFO_FMODE=0o0600
# The permissiosn mode for the socket file directory, if using UDS.
#CINFO_DMODE=0o0700
# The GID or group name of the group to chown the socket to.
# Uses the runtime user UID if not specified.
#CINFO_FGRP=http
# The GID or group name of the group to chown the socket directory to.
# Uses the primary group of the runtime user if not specified.
#CINFO_DGRP=http
# If specified, IPv4 clients can have their IPv6 address detected
# by using this URL (e.g. 'https://c6.r00t2.io/').
# It is expected that this URL is also serving ClientInfo.
#CINFO_V6U='https://c6.r00t2.io/'
# If specified, IPv6 clients can have their IPv4 address detected
# by using this URL (e.g. 'https://c4.r00t2.io/').
# It is expected that this URL is also serving ClientInfo.
#CINFO_V4U='https://c4.r00t2.io/'
# Specify additional/explicit CORS origin(s).
# If you're using -4/--ipv4/$CINFO_V4U and/or
# -6/--ipv6/$CINFO_V6U, they will be added
# automatically but you should add any
# dual-stack domains that serve ClientInfo here,
# separated by commas.
# (e.g. 'https://c.r00t2.io'
#CINFO_CORS='https://c.r00t2.io'

View File

@@ -9,7 +9,7 @@ Description=Return Client Request Information
Type=notify
# These may also be "httpd", "nginx", etc.
User=http
Group=htttp
Group=http
Restart=on-failure
RestartSec=10
# Not required, but you may create this file and specify the associated commandline argument env vars.
@@ -17,6 +17,7 @@ RestartSec=10
# See clientinfo.env for an example.
EnvironmentFile=-/etc/default/clientinfo
ExecStart=/usr/local/bin/clientinfo
RuntimeDirectory=clientinfo
[Install]
WantedBy=multi-user.target

View File

@@ -1,7 +1,7 @@
package args
import (
`io/fs`
"io/fs"
)
type Args struct {
@@ -12,11 +12,14 @@ type Args struct {
SockDirMode fs.FileMode `env:"CINFO_DMODE" short:"M" long:"dmode" default:"0o0700" description:"If using a UDS, attempt to set the directory containing the socket to use this permission. This should probably be either 0o0700 or 0o0770."`
SockGrp *string `env:"CINFO_FGRP" short:"g" long:"fgroup" description:"If specified and using a UDS, attempt to set the socket to this GID/group name. (If unspecified, the default is current user's primary group.)"`
SockDirGrp *string `env:"CINFO_DGRP" short:"G" long:"dgroup" description:"If specified and using a UDS, attempt to set the directory containing the socket to this GID/group name. (If unspecified, the default is current user's primary group.)"`
V4Url *string `env:"CINFO_V4U" short:"4" long:"ipv4" description:"If specified, IPv6 clients can have their IPv4 address detected by using this URL (e.g. 'https://c4.r00t2.io/').\nIt is expected that this URL is also serving ClientInfo." validate:"omitempty,url"`
V6Url *string `env:"CINFO_V6U" short:"6" long:"ipv6" description:"If specified, IPv4 clients can have their IPv6 address detected by using this URL (e.g. 'https://c6.r00t2.io/').\nIt is expected that this URL is also serving ClientInfo." validate:"omitempty,url"`
CORS []string `env:"CINFO_CORS" short:"c" long:"cors" description:"Specify additional/explicit CORS origin(s). If you're using -4/--ipv4 and/or -6/--ipv6, they will be added automatically but you should add any dual-stack domains that serve ClientInfo here (e.g. '--cors https://r00t2.io'"`
Listen ListenArgs `positional-args:"true"`
}
type ListenArgs struct {
Listen string `env:"CINFO_URI" positional-arg-name:"LISTEN_URI" default:"unix:///var/run/clientinfo/fcgi.sock" description:"The specification to listen on.\nIf the scheme is 'unix', a FastCGI UDS/IPC socket is used (default); any host, query parameters, etc. component is ignored and the URI path is used to specify the socket.\nIf 'tcp', a FastCGI socket over TCP is opened on the <host:port>.\nIf 'http', an HTTP listener is opened on the <host:port>; any path, query parameters, etc. components are ignored.\nHTTPS is unsupported; terminate with a reverse proxy. All other schemes will cause a fatal error.\nThe default is 'unix:///var/run/clientinfo/fcgi.sock'." validate:"required,uri"`
Listen string `env:"CINFO_URI" positional-arg-name:"LISTEN_URI" default:"unix:///var/run/clientinfo/fcgi.sock" description:"The specification to listen on.\nIf the scheme is 'unix', a FastCGI UDS/IPC socket is used (default); any host, query parameters, etc. component is ignored and the URI path is used to specify the socket.\nIf 'tcp', a FastCGI socket over TCP is opened on the <host:port>.\nIf 'http', an HTTP listener is opened on the <host:port>; any path, query parameters, etc. components are ignored.\nHTTPS is unsupported; terminate with a reverse proxy. All other schemes will cause a fatal error." validate:"required,uri"`
}
type UdsPerms struct {

24
go.mod
View File

@@ -1,30 +1,30 @@
module r00t2.io/clientinfo
go 1.23.3
go 1.25.5
require (
github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf
github.com/creasty/defaults v1.8.0
github.com/davecgh/go-spew v1.1.1
github.com/go-playground/validator/v10 v10.23.0
github.com/goccy/go-yaml v1.15.7
github.com/go-playground/validator/v10 v10.29.0
github.com/goccy/go-yaml v1.19.0
github.com/jessevdk/go-flags v1.6.1
github.com/mileusna/useragent v1.3.5
golang.org/x/mod v0.22.0
r00t2.io/goutils v1.7.1
r00t2.io/sysutils v1.12.0
golang.org/x/mod v0.31.0
r00t2.io/goutils v1.13.0
r00t2.io/sysutils v1.15.0
)
require (
github.com/coreos/go-systemd/v22 v22.6.0 // indirect
github.com/djherbis/times v1.6.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.7 // indirect
github.com/gabriel-vasile/mimetype v1.4.12 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
golang.org/x/crypto v0.30.0 // indirect
golang.org/x/net v0.32.0 // indirect
golang.org/x/sync v0.10.0 // indirect
golang.org/x/sys v0.28.0 // indirect
golang.org/x/text v0.21.0 // indirect
golang.org/x/crypto v0.46.0 // indirect
golang.org/x/sync v0.19.0 // indirect
golang.org/x/sys v0.39.0 // indirect
golang.org/x/text v0.32.0 // indirect
)

59
go.sum
View File

@@ -1,27 +1,25 @@
github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf h1:iW4rZ826su+pqaw19uhpSCzhj44qo35pNgKFGqzDKkU=
github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/coreos/go-systemd/v22 v22.6.0 h1:aGVa/v8B7hpb0TKl0MWoAavPDmHvobFe5R5zn0bCJWo=
github.com/coreos/go-systemd/v22 v22.6.0/go.mod h1:iG+pp635Fo7ZmV/j14KUcmEyWF+0X7Lua8rrTWzYgWU=
github.com/creasty/defaults v1.8.0 h1:z27FJxCAa0JKt3utc0sCImAEb+spPucmKoOdLHvHYKk=
github.com/creasty/defaults v1.8.0/go.mod h1:iGzKe6pbEHnpMPtfDXZEr0NVxWnPTjb1bbDy08fPzYM=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/djherbis/times v1.6.0 h1:w2ctJ92J8fBvWPxugmXIv7Nz7Q3iDMKNx9v5ocVH20c=
github.com/djherbis/times v1.6.0/go.mod h1:gOHeRAz2h+VJNZ5Gmc/o7iD9k4wW7NMVqieYCY99oc0=
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
github.com/gabriel-vasile/mimetype v1.4.7 h1:SKFKl7kD0RiPdbht0s7hFtjl489WcQ1VyPW8ZzUMYCA=
github.com/gabriel-vasile/mimetype v1.4.7/go.mod h1:GDlAgAyIRT27BhFl53XNAFtfjzOkLaF35JdEG0P7LtU=
github.com/gabriel-vasile/mimetype v1.4.12 h1:e9hWvmLYvtp846tLHam2o++qitpguFiYCKbn0w9jyqw=
github.com/gabriel-vasile/mimetype v1.4.12/go.mod h1:d+9Oxyo1wTzWdyVUPMmXFvp4F9tea18J8ufA774AB3s=
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.23.0 h1:/PwmTwZhS0dPkav3cdK9kV1FsAmrL8sThn8IHr/sO+o=
github.com/go-playground/validator/v10 v10.23.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
github.com/goccy/go-yaml v1.15.7 h1:L7XuKpd/A66X4w/dlk08lVfiIADdy79a1AzRoIefC98=
github.com/goccy/go-yaml v1.15.7/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/go-playground/validator/v10 v10.29.0 h1:lQlF5VNJWNlRbRZNeOIkWElR+1LL/OuHcc0Kp14w1xk=
github.com/go-playground/validator/v10 v10.29.0/go.mod h1:D6QxqeMlgIPuT02L66f2ccrZ7AGgHkzKmmTMZhk/Kc4=
github.com/goccy/go-yaml v1.19.0 h1:EmkZ9RIsX+Uq4DYFowegAuJo8+xdX3T/2dwNPXbxEYE=
github.com/goccy/go-yaml v1.19.0/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/jessevdk/go-flags v1.6.1 h1:Cvu5U8UGrLay1rZfv/zP7iLpSHGUZ/Ou68T0iX1bBK4=
@@ -34,33 +32,20 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/crypto v0.19.0 h1:ENy+Az/9Y1vSrlrvBSyna3PITt4tiZLf7sgCjZBX7Wo=
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
golang.org/x/crypto v0.30.0 h1:RwoQn3GkWiMkzlX562cLB7OxWvjH1L8xutO2WoJcRoY=
golang.org/x/crypto v0.30.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
golang.org/x/mod v0.22.0 h1:D4nJWe9zXqHOmWqj4VMOJhvzj7bEZg4wEYa759z1pH4=
golang.org/x/mod v0.22.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/net v0.32.0 h1:ZqPmj8Kzc+Y6e0+skZsuACbx+wzMgo5MQsJh9Qd6aYI=
golang.org/x/net v0.32.0/go.mod h1:CwU0IoeOlnQQWJ6ioyFrfRuomB8GKF6KbYXZVyeXNfs=
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/crypto v0.46.0 h1:cKRW/pmt1pKAfetfu+RCEvjvZkA9RimPbh7bhFjGVBU=
golang.org/x/crypto v0.46.0/go.mod h1:Evb/oLKmMraqjZ2iQTwDwvCtJkczlDuTmdJXoZVzqU0=
golang.org/x/mod v0.31.0 h1:HaW9xtz0+kOcWKwli0ZXy79Ix+UW/vOfmWI5QVd2tgI=
golang.org/x/mod v0.31.0/go.mod h1:43JraMp9cGx1Rx3AqioxrbrhNsLl2l/iNAvuBkrezpg=
golang.org/x/sync v0.19.0 h1:vV+1eWNmZ5geRlYjzm2adRgW2/mcpevXNg50YZtPCE4=
golang.org/x/sync v0.19.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
golang.org/x/sys v0.0.0-20220615213510-4f61da869c0c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
golang.org/x/sys v0.39.0 h1:CvCKL8MeisomCi6qNZ+wbb0DN9E5AATixKsvNtMoMFk=
golang.org/x/sys v0.39.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
golang.org/x/text v0.32.0 h1:ZD01bjUt1FQ9WJ0ClOL5vxgxOI/sVCNgX1YtKwcY0mU=
golang.org/x/text v0.32.0/go.mod h1:o/rUWzghvpD5TXrTIBuJU77MTaN0ljMWE47kxGJQ7jY=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
r00t2.io/goutils v1.7.1 h1:Yzl9rxX1sR9WT0FcjK60qqOgBoFBOGHYKZVtReVLoQc=
r00t2.io/goutils v1.7.1/go.mod h1:9ObJI9S71wDLTOahwoOPs19DhZVYrOh4LEHmQ8SW4Lk=
r00t2.io/sysutils v1.1.1 h1:q2P5u50HIIRk6muCPo1Gpapy6sNT4oaB1l2O/C/mi3A=
r00t2.io/sysutils v1.1.1/go.mod h1:Wlfi1rrJpoKBOjWiYM9rw2FaiZqraD6VpXyiHgoDo/o=
r00t2.io/sysutils v1.12.0 h1:Ce3qUOyLixE1ZtFT/+SVwOT5kSkzg5+l1VloGeGugrU=
r00t2.io/sysutils v1.12.0/go.mod h1:bNTKNBk9MnUhj9coG9JBNicSi5FrtJHEM645um85pyw=
r00t2.io/goutils v1.13.0 h1:103QR2eUu42TNF7h9r6YgahbSgRAJ8VtFxSzw9rMaXI=
r00t2.io/goutils v1.13.0/go.mod h1:/0M+6fW2VAgdtAvwGE9oNV259MoEZeJ84rLCZsxe8uI=
r00t2.io/sysutils v1.15.0 h1:FSnREfbXDhBQEO7LMpnRQeKlPshozxk9XHw3YgWRgRg=
r00t2.io/sysutils v1.15.0/go.mod h1:28qB0074EIRQ8Sy/ybaA5jC3qA32iW2aYLkMCRhyAFM=

View File

@@ -24,7 +24,7 @@ const (
trueUaFieldStr string = "Yes"
falseUaFieldStr string = "No"
dfltIndent string = " "
httpRealHdr string = "X-ClientInfo-RealIP"
httpRealHdr string = "X-ClientInfo-RealIP" // TODO: advise https://nginx.org/en/docs/http/ngx_http_realip_module.html NGINX module? Allow config for user-specified header?
)
var (
@@ -35,6 +35,8 @@ var (
Funcs(
template.FuncMap{
"getTitle": getTitle,
"getIpver": getIpver,
"safeUrl": safeUrl,
},
).ParseFS(tplDir, "tpl/*.tpl"),
)
@@ -79,10 +81,15 @@ var (
mediaJSON: json.MarshalIndent,
mediaXML: xml.MarshalIndent,
}
// valid MIMEs.
okAcceptMime []string = []string{
mediaJSON,
mediaXML,
mediaYAML,
mediaHTML,
}
// These are actually HTML.
htmlOverride map[string]bool = map[string]bool{
"Links": true,
}
)

View File

@@ -46,6 +46,7 @@ func NewClient(uaStr string) (r *R00tClient, err error) {
func NewServer(log logging.Logger, cliArgs *args.Args) (srv *Server, err error) {
var s Server
var origin string
var udsSockPerms args.UdsPerms
if log == nil {
@@ -62,6 +63,7 @@ func NewServer(log logging.Logger, cliArgs *args.Args) (srv *Server, err error)
args: cliArgs,
mux: http.NewServeMux(),
sock: nil,
corsOrigins: nil,
reloadChan: make(chan os.Signal),
stopChan: make(chan os.Signal),
}
@@ -73,6 +75,42 @@ func NewServer(log logging.Logger, cliArgs *args.Args) (srv *Server, err error)
s.mux.HandleFunc("/usage.html", s.handleUsage)
s.mux.HandleFunc("/favicon.ico", s.explicit404)
if cliArgs.CORS != nil && len(cliArgs.CORS) > 0 {
if s.corsOrigins == nil {
s.corsOrigins = make(map[string]struct{})
}
for _, origin = range cliArgs.CORS {
s.corsOrigins[strings.ToLower(origin)] = struct{}{}
}
}
if cliArgs.V4Url != nil {
if s.v4Url, err = url.Parse(*cliArgs.V4Url); err != nil {
s.log.Err("server.NewServer: Failed to parse IPv4 URI '%s': %v", *cliArgs.V4Url, err)
return
}
if s.corsOrigins == nil {
s.corsOrigins = make(map[string]struct{})
}
origin = strings.ToLower(fmt.Sprintf("%s://%s", s.v4Url.Scheme, s.v4Url.Host))
s.corsOrigins[origin] = struct{}{}
}
if cliArgs.V6Url != nil {
if s.v6Url, err = url.Parse(*cliArgs.V6Url); err != nil {
s.log.Err("server.NewServer: Failed to parse IPv6 URI '%s': %v", *cliArgs.V6Url, err)
return
}
if s.corsOrigins == nil {
s.corsOrigins = make(map[string]struct{})
}
origin = strings.ToLower(fmt.Sprintf("%s://%s", s.v6Url.Scheme, s.v6Url.Host))
s.corsOrigins[origin] = struct{}{}
}
if s.corsOrigins != nil {
s.log.Debug("server.NewServer: CORS origins: %#v", s.corsOrigins)
}
if s.listenUri, err = url.Parse(cliArgs.Listen.Listen); err != nil {
s.log.Err("server.NewServer: Failed to parse listener URI: %v", err)
return

View File

@@ -4,6 +4,48 @@ import (
`fmt`
)
func (p *Page) AltURL() (altUrl string) {
if !p.HasAltURL() {
return
}
if p.Info.IP.Is4() {
altUrl = p.srv.v6Url.String()
} else if p.Info.IP.Is6() {
altUrl = p.srv.v4Url.String()
}
return
}
func (p *Page) AltVer() (altVer string) {
if !p.HasAltURL() {
return
}
if p.Info.IP.Is4() {
altVer = "v6"
} else if p.Info.IP.Is6() {
altVer = "v4"
}
return
}
func (p *Page) HasAltURL() (hasAlt bool) {
if p.Info == nil {
return
}
hasAlt = (p.Info.IP.Is4() && p.srv.v6Url != nil) ||
(p.Info.IP.Is6() && p.srv.v4Url != nil)
return
}
func (p *Page) RenderIP(indent uint) (s string) {
s = fmt.Sprintf("<a href=\"https://ipinfo.io/%s\">%s</a>", p.Info.IP.String(), p.Info.IP.String())

View File

@@ -3,23 +3,24 @@ package server
import (
`encoding/json`
`encoding/xml`
"errors"
"fmt"
"net"
"net/http"
`errors`
`fmt`
`net`
`net/http`
`net/http/fcgi`
"net/netip"
"net/url"
"os"
"os/signal"
"strings"
"sync"
`net/netip`
`net/url`
`os`
`os/signal`
`strings`
`sync`
`syscall`
sysd "github.com/coreos/go-systemd/daemon"
"github.com/davecgh/go-spew/spew"
sysd `github.com/coreos/go-systemd/daemon`
`github.com/davecgh/go-spew/spew`
`github.com/goccy/go-yaml`
"r00t2.io/goutils/multierr"
`r00t2.io/clientinfo/version`
`r00t2.io/goutils/multierr`
)
// Close cleanly closes any remnants of a Server. Stop should be used instead to cleanly shut down; this is a little more aggressive.
@@ -266,8 +267,10 @@ func (s *Server) explicit404(resp http.ResponseWriter, req *http.Request) {
func (s *Server) handleDefault(resp http.ResponseWriter, req *http.Request) {
var err error
var ok bool
var page *Page
var uas []string
var origin string
var reqdMimes []string
var parsedUA *R00tClient
var nAP netip.AddrPort
@@ -279,11 +282,20 @@ func (s *Server) handleDefault(resp http.ResponseWriter, req *http.Request) {
var outerFmt string = mediaJSON
s.log.Debug("server.Server.handleDefault: Handling request:\n%s", spew.Sdump(req))
origin = req.Header.Get("Origin")
if s.corsOrigins != nil && len(s.corsOrigins) != 0 && origin != "" {
if _, ok = s.corsOrigins[origin]; ok {
resp.Header().Set("Access-Control-Allow-Origin", origin)
resp.Header().Add("Vary", "Origin")
}
}
resp.Header().Set("ClientInfo-Version", version.Ver.Short())
page = &Page{
Info: &R00tInfo{
Client: nil,
IP: nil,
IP: netip.Addr{},
Port: 0,
Headers: XmlHeaders(req.Header),
Req: req,
@@ -298,7 +310,7 @@ func (s *Server) handleDefault(resp http.ResponseWriter, req *http.Request) {
// First the client info.
remAddrPort = req.RemoteAddr
if s.isHttp && req.Header.Get(httpRealHdr) != "" {
// TODO: WHitelist explicit reverse proxy addr(s)?
// TODO: Whitelist explicit reverse proxy addr(s)?
remAddrPort = req.Header.Get(httpRealHdr)
req.Header.Del(httpRealHdr)
}
@@ -312,7 +324,7 @@ func (s *Server) handleDefault(resp http.ResponseWriter, req *http.Request) {
*/
err = nil
}
page.Info.IP = net.ParseIP(nAP.Addr().String())
page.Info.IP = nAP.Addr().Unmap()
page.Info.Port = nAP.Port()
}
if req.URL != nil {
@@ -327,6 +339,9 @@ func (s *Server) handleDefault(resp http.ResponseWriter, req *http.Request) {
http.Error(resp, fmt.Sprintf("ERROR: Failed to parse 'User-Agent' '%s'", ua), http.StatusInternalServerError)
return
}
if parsedUA.Name != nil && htmlOverride[*parsedUA.Name] {
parsedUA.IsDesktop = true
}
page.Info.Client = append(page.Info.Client, parsedUA)
}
}
@@ -470,6 +485,7 @@ func (s *Server) handleAbout(resp http.ResponseWriter, req *http.Request) {
s.log.Debug("server.Server.handleAbout: Handling request:\n%s", spew.Sdump(req))
resp.Header().Set("ClientInfo-Version", version.Ver.Short())
resp.Header().Set("Content-Type", "text/html; charset=utf-8")
if err = tpl.ExecuteTemplate(resp, "about", renderPage); err != nil {
@@ -495,7 +511,9 @@ func (s *Server) handleUsage(resp http.ResponseWriter, req *http.Request) {
s.log.Debug("server.Server.handleUsage: Handling request:\n%s", spew.Sdump(req))
resp.Header().Set("ClientInfo-Version", version.Ver.Short())
resp.Header().Set("Content-Type", "text/html; charset=utf-8")
if err = tpl.ExecuteTemplate(resp, "usage", renderPage); err != nil {
s.log.Err("server.Server.handleAbout: Failed to execute template for '%s': %v", req.RemoteAddr, err)
http.Error(resp, "ERROR: Failed to render HTML", http.StatusInternalServerError)
@@ -511,6 +529,8 @@ func (s *Server) renderJSON(page *Page, resp http.ResponseWriter) (err error) {
var b []byte
resp.Header().Set("Content-Type", "application/json")
if page.DoIndent {
if b, err = json.MarshalIndent(page.Info, "", page.Indent); err != nil {
s.log.Err("server.Server.renderJSON: Failed to render to indented JSON: %v", err)
@@ -536,6 +556,8 @@ func (s *Server) renderHTML(page *Page, resp http.ResponseWriter) (err error) {
var b []byte
page.srv = s
if page.RawFmt != nil {
switch *page.RawFmt {
case mediaHTML:
@@ -561,10 +583,14 @@ func (s *Server) renderHTML(page *Page, resp http.ResponseWriter) (err error) {
s.log.Err("server.Server.renderHTML: Failed to render to '%s': %v", *page.RawFmt, err)
}
}
page.Raw = new(string)
*page.Raw = string(b)
if b != nil {
page.Raw = new(string)
*page.Raw = string(b)
}
}
resp.Header().Set("Content-Type", "text/html; charset=utf-8")
if err = tpl.ExecuteTemplate(resp, "index", page); err != nil {
s.log.Err("server.Server.renderHTML: Failed to render template: %v", err)
http.Error(resp, "ERROR: Failed to render HTML", http.StatusInternalServerError)
@@ -591,6 +617,9 @@ func (s *Server) renderXML(page *Page, resp http.ResponseWriter) (err error) {
return
}
}
resp.Header().Set("Content-Type", "application/xml; charset=utf-8")
if _, err = resp.Write(b); err != nil {
s.log.Err("server.Server.renderXML: Failed to send XML: %v", err)
return
@@ -609,6 +638,8 @@ func (s *Server) renderYML(page *Page, resp http.ResponseWriter) (err error) {
return
}
resp.Header().Set("Content-Type", "application/yaml")
if _, err = resp.Write(b); err != nil {
s.log.Err("server.Server.renderJSON: Failed to send JSON: %v", err)
return

View File

@@ -2,9 +2,22 @@ package server
import (
`fmt`
`net/netip`
`strings`
`html/template`
)
func getIpver(a netip.Addr) (verStr string) {
if a.Is4() {
verStr = "v4"
} else if a.Is6() {
verStr = "v6"
}
return
}
func getTitle(subPage string) (title string) {
if subPage == "" || subPage == "index" {
@@ -16,3 +29,10 @@ func getTitle(subPage string) (title string) {
return
}
func safeUrl(urlStr string) (u template.URL) {
u = template.URL(urlStr)
return
}

View File

@@ -4,9 +4,35 @@
{{- $linkico := "🔗" }}
<h2 id="client">Client/Browser Information<a href="#client">{{ $linkico }}</a></h2>
<p>
<b>Your IP Address is <i><a href="https://ipinfo.io/{{ $page.Info.IP.String }}">{{ $page.Info.IP.String }}</a></i>.</b>
<b>Your IP{{ getIpver $page.Info.IP }} Address is <i><a href="https://ipinfo.io/{{ $page.Info.IP.String }}">{{ $page.Info.IP.String }}</a></i>.</b>
<br/>
<i>You are connecting with port <b>{{ $page.Info.Port }}</b> outbound.</i>
{{- if $page.HasAltURL }}
<div id="alt_addr"></div>
<script>
async function fetchAltAddr() {
try {
const resp = await fetch('{{ safeUrl $page.AltURL }}', {
headers: {
'Accept': 'application/json'
}
});
if (resp.ok) {
const dat = await resp.json();
const addr = dat.ip;
const infoDiv = document.getElementById('alt_addr');
infoDiv.innerHTML = `
<i>You also have IP{{ $page.AltVer }} address <b><a href="https://ipinfo.io/${addr}">${addr}</a></b>.</i>
<br/>
<i>Try loading <b><a href="{{ $page.AltURL }}">{{ $page.AltURL }}</a></b> for more information.</i>`;
}
} catch (error) {
console.info('Did not fetch alternate address: ', error);
}
}
fetchAltAddr()
</script>
{{- end }}
</p>
{{- if $page.Raw }}
<h3 id="client_raw">Raw Block ({{ $page.RawFmt }})<a href="#client_raw">{{ $linkico }}</a></h3>
@@ -42,7 +68,7 @@
</tbody>
</table>
</div>
{{- /*
{{- /*
<ul>
{{- $flds := $ua.ToMap }}

View File

@@ -46,8 +46,8 @@ Accept: text/html
</pre>
</p>
Note that <a href="https://lynx.invisible-island.net/">Lynx</a> and <a href="http://elinks.or.cz/">Elinks</a> are considered "graphical"
browsers by this program as they are HTML-centric.
Note that <a href="http://links.twibright.com/">Links</a>, <a href="https://lynx.invisible-island.net/">Lynx</a>, <a href="http://elinks.or.cz/">Elinks</a>,
and <a href="https://w3m.sourceforge.net/">W3M</a> are considered "graphical" browsers by this program as they are HTML-centric.
</p>
<p id="usage_params_mod">
The following parameters control/modify behavior.<a href="#usage_params_mod">{{ $linkico }}</a>

View File

@@ -1,19 +1,22 @@
package server
import (
`encoding/xml`
`net`
`net/http`
`net/url`
`os`
"encoding/xml"
"net"
"net/http"
"net/netip"
"net/url"
"os"
`github.com/mileusna/useragent`
`r00t2.io/clientinfo/args`
`r00t2.io/goutils/logging`
"github.com/mileusna/useragent"
"r00t2.io/clientinfo/args"
"r00t2.io/goutils/logging"
)
type outerRenderer func(page *Page, resp http.ResponseWriter) (err error)
type XmlHeaders map[string][]string
type (
outerRenderer func(page *Page, resp http.ResponseWriter) (err error)
XmlHeaders map[string][]string
)
// R00tInfo is the structure of data returned to the client.
type R00tInfo struct {
@@ -22,7 +25,7 @@ type R00tInfo struct {
// Client is the UA/Client info, if any passed by the client.
Client []*R00tClient `json:"ua,omitempty" xml:"ua,omitempty" yaml:"Client/User Agent,omitempty"`
// IP is the client IP address.
IP net.IP `json:"ip" xml:"ip,attr" yaml:"Client IP Address"`
IP netip.Addr `json:"ip" xml:"ip,attr" yaml:"Client IP Address"`
// Port is the client's port number.
Port uint16 `json:"port" xml:"port,attr" yaml:"Client Port"`
// Headers are the collection of the request headers sent by the client.
@@ -84,19 +87,23 @@ type Page struct {
Indent string
// DoIndent indicates if indenting was enabled.
DoIndent bool
srv *Server
}
type Server struct {
log logging.Logger
args *args.Args
listenUri *url.URL
isHttp bool
mux *http.ServeMux
sock net.Listener
doneChan chan bool
stopChan chan os.Signal
reloadChan chan os.Signal
isStopping bool
log logging.Logger
args *args.Args
listenUri *url.URL
isHttp bool
mux *http.ServeMux
sock net.Listener
doneChan chan bool
stopChan chan os.Signal
reloadChan chan os.Signal
v4Url *url.URL
v6Url *url.URL
corsOrigins map[string]struct{}
isStopping bool
}
// https://www.iana.org/assignments/media-types/media-types.xhtml