server

package
v2.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 9, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package server implements an HTTPS web server. The configuration is idiosyncratic, and not intended to be suitable for everyone.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Country

func Country(r *http.Request) (loc string)

Country returns the location country code for the current request.

func Location

func Location(r *http.Request) (loc string)

Location returns both the registered and location country codes for the current request, if they are different.

func Registered

func Registered(r *http.Request) (loc string)

Registered returns the registered country codes for the current request.

func RemoteIP

func RemoteIP(r *http.Request) (ip string)

RemoteIP returns the originating IP address for the current request.

Types

type App

type App interface {

	// Routes registers handlers for web request paths.
	Routes() http.Handler
}

App is the interface provided by the web application.

type GeoBlocker

type GeoBlocker struct {
	Allow        bool // permit only specified countries, instead of blocking them
	ErrorLog     *log.Logger
	Reporter     func(r *http.Request, location string, ip net.IP) string
	ReportSingle bool   // report just location or registered country, not both
	Store        string // storage location for database
	// contains filtered or unexported fields
}

GeoBlocker holds the parameters and state for geo-blocking. Typically only one is needed.

func (*GeoBlocker) GeoBlock

func (gb *GeoBlocker) GeoBlock(next http.Handler) http.Handler

GeoBlock initialises and returns a handler to block IPs for some locations.

func (*GeoBlocker) Locate

func (gb *GeoBlocker) Locate(ipStr string) (country, registered string, ip net.IP)

Locate looks up a remote address in the geolocation database, and returns the countries of origin and registration.

func (*GeoBlocker) RejectsCounted

func (gb *GeoBlocker) RejectsCounted() (rejects int)

RejectsCounted returns a statistic of the total number of requests rejected, and resets the count.

func (*GeoBlocker) Start

func (gb *GeoBlocker) Start(countries []string)

Start initialises the geo-blocker.

func (*GeoBlocker) Stop

func (gb *GeoBlocker) Stop()

Stop ends geo-blocking.

type Server

type Server struct {

	// logging
	ErrorLog *log.Logger
	InfoLog  *log.Logger

	// HTTPS
	CertEmail string   // notifications from Let's Encrypt
	CertPath  string   // folder for certificates
	Domains   []string // domains to be served (empty for HTTP)

	// port addresses
	AddrHTTP  string
	AddrHTTPS string
}

Server specifies the parameters for a web server.

func (*Server) Serve

func (srv *Server) Serve(app App)

Serve runs the web server. It never returns.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL