mirror of
https://github.com/D-Modem/WebOne-go.git
synced 2026-11-28 12:38:05 -08:00
A Go translation of WebOne (github.com/atauenis/webone, commit e940b43), the HTTP 1.x proxy that lets old browsers use today's web: config loader with sets of edits and .NET regular expressions, the HTTP/1.0-1.1 server with keep-alive, HTTPS fetching and patching, code pages, the CONNECT proxy (MITM, pass-through, non-HTTP tunnels), the CA and site certificates (MD5/SHA-1 signing included), internal and error pages, Web Archive fallback, converters, web FTP client and online video. WebOne's licence, configuration files and html pages are kept. Go-only options: SslCaDirectory signs with the D-Modem BBS's proxy sub-CA (or its root), SslTrustOwnCA, and DnsServer. README lists the differences: no SSL 2.0/3.0 in Go's TLS, and the upstream bugs fixed on purpose. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01B82eRRH4kntyPfrY1bFWC3
138 lines
3.3 KiB
Go
138 lines
3.3 KiB
Go
// Package logx is WebOne's event log: lines go to the console (unless the
|
|
// proxy runs as a daemon) and to the log file, each request's lines
|
|
// stamped with the time the request began and the time since.
|
|
//
|
|
// Ported from WebOne's Log.cs.
|
|
package logx
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"runtime"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
const stampLayout = "02.01.2006 15:04:05.000"
|
|
|
|
var (
|
|
mu sync.Mutex
|
|
file io.WriteCloser
|
|
console io.Writer = os.Stdout
|
|
// Daemon hides console output (all goes to the log file only).
|
|
Daemon bool
|
|
)
|
|
|
|
// SetConsole sends console output to w (tests).
|
|
func SetConsole(w io.Writer) {
|
|
mu.Lock()
|
|
console = w
|
|
mu.Unlock()
|
|
}
|
|
|
|
// OpenLogFile starts writing the log to path (appending when append is set).
|
|
// An empty path stops file logging.
|
|
func OpenLogFile(path string, appendTo bool, version string, args []string) {
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
if file != nil {
|
|
file.Close()
|
|
file = nil
|
|
}
|
|
if path == "" {
|
|
fmt.Fprintln(console, "Not using log file.")
|
|
return
|
|
}
|
|
flags := os.O_CREATE | os.O_WRONLY
|
|
if appendTo {
|
|
flags |= os.O_APPEND
|
|
} else {
|
|
flags |= os.O_TRUNC
|
|
}
|
|
f, err := os.OpenFile(path, flags, 0o644)
|
|
if err != nil {
|
|
fmt.Fprintf(console, "Cannot use log file %s: %v.\n", path, err)
|
|
return
|
|
}
|
|
file = f
|
|
fmt.Fprintf(f, "%s\tWebOne %s%s (%s %s, %s) log started.\n",
|
|
time.Now().Format("02.01.2006 15:04:05"), version, " "+strings.Join(args, " "),
|
|
runtime.GOOS, runtime.GOARCH, runtime.Version())
|
|
fmt.Fprintf(console, "Using event log file %s.\n", path)
|
|
}
|
|
|
|
// Enabled: a log file is open.
|
|
func Enabled() bool {
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
return file != nil
|
|
}
|
|
|
|
// Close closes the log file.
|
|
func Close() {
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
if file != nil {
|
|
file.Close()
|
|
file = nil
|
|
}
|
|
}
|
|
|
|
func emit(msg string, display, write bool, displayText string) {
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
if display && !Daemon {
|
|
if displayText == "" {
|
|
fmt.Fprintln(console, msg)
|
|
} else {
|
|
fmt.Fprintln(console, displayText)
|
|
}
|
|
}
|
|
if write && file != nil {
|
|
fmt.Fprintln(file, msg)
|
|
}
|
|
}
|
|
|
|
// Writer logs the lines of one request (or of the server itself).
|
|
type Writer struct {
|
|
Begin time.Time
|
|
first bool
|
|
}
|
|
|
|
// New is a Writer whose time starts now.
|
|
func New() *Writer { return &Writer{Begin: time.Now(), first: true} }
|
|
|
|
// Std is the server's own log writer.
|
|
var Std = New()
|
|
|
|
func (w *Writer) since() string {
|
|
ticks := time.Since(w.Begin).Nanoseconds() / 100
|
|
return fmt.Sprintf("%s+%d", w.Begin.Format(stampLayout), ticks/2)
|
|
}
|
|
|
|
// Printf logs a request line, displayed and timestamped.
|
|
func (w *Writer) Printf(format string, args ...any) { w.Log(true, true, format, args...) }
|
|
|
|
// Log writes a line. display: also to the console. stamped: a request line
|
|
// (">GET http://...") rather than a server message ("Starting ...").
|
|
func (w *Writer) Log(display, stamped bool, format string, args ...any) {
|
|
msg := fmt.Sprintf(format, args...)
|
|
begin := w.Begin.Format(stampLayout)
|
|
if !stamped {
|
|
emit(begin+"+0\t"+msg, display, true, msg)
|
|
return
|
|
}
|
|
if w.first {
|
|
w.first = false
|
|
emit(begin+"+0\t\t"+msg, display, false, "")
|
|
emit(begin+"+0\t"+msg, false, true, "")
|
|
return
|
|
}
|
|
emit(w.since()+"\t"+msg, display, true, "")
|
|
}
|
|
|
|
// Server logs a server message (not a request line).
|
|
func Server(display bool, format string, args ...any) { Std.Log(display, false, format, args...) }
|