1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
|
// mail2news v1.0.5 - Privacy-Enhanced Mail to Usenet Gateway
// Accepts both:
// - mail2news-YYYYMMDD-newsgroup@domain (classic format)
// - mail2news@domain (reads Newsgroups: from header)
// Copyright 2025
//
// v1.0.5 changelog:
// - Removed adaptive padding (pointless on cleartext NNTP traffic)
// - Removed StripPadding (was dead code, never called)
// - Cleaned padding-related constants and config fields
package main
import (
"bufio"
"bytes"
"crypto/rand"
"crypto/sha256"
"crypto/subtle"
"encoding/base64"
"encoding/hex"
"fmt"
"io"
"log"
"math/big"
"mime"
"mime/quotedprintable"
"net"
"net/mail"
"net/textproto"
"os"
"path/filepath"
"regexp"
"runtime"
"sort"
"strings"
"sync"
"time"
"github.com/spf13/viper"
"golang.org/x/net/proxy"
"golang.org/x/text/encoding/charmap"
)
const (
MaxMessageSize = 1024 * 1024 * 10 // 10MB
CacheExpiration = 24 * time.Hour
MinDelayMs = 50
MaxDelayMs = 500
JitterMs = 100
ExitOK = 0
ExitError = 1
ExitReject = 2
ExitDuplicate = 3
)
// Config structures
type Config struct {
Paths PathsConfig `mapstructure:"paths"`
NNTP NNTPConfig `mapstructure:"nntp"`
Thresholds ThresholdsConfig `mapstructure:"thresholds"`
Logging LoggingConfig `mapstructure:"logging"`
Encoding EncodingConfig `mapstructure:"encoding"`
Privacy PrivacyConfig `mapstructure:"privacy"`
Headers HeadersConfig `mapstructure:"headers"`
}
type PathsConfig struct {
Log string `mapstructure:"log"`
Etc string `mapstructure:"etc"`
Lib string `mapstructure:"lib"`
History string `mapstructure:"history"`
}
type NNTPConfig struct {
PathHeader string `mapstructure:"path_header"`
InjectionHost string `mapstructure:"injection_host"`
Contact string `mapstructure:"contact"`
MessageIDDomain string `mapstructure:"messageid_domain"`
DefaultFrom string `mapstructure:"default_from"`
OnionServers []string `mapstructure:"onion_servers"`
ClearnetServers []string `mapstructure:"clearnet_servers"`
AlwaysUseTor bool `mapstructure:"always_use_tor"`
TorProxy string `mapstructure:"tor_proxy"`
}
type ThresholdsConfig struct {
MaxBytes int `mapstructure:"max_bytes"`
MaxCrossposts int `mapstructure:"max_crossposts"`
HoursPast int `mapstructure:"hours_past"`
HoursFuture int `mapstructure:"hours_future"`
SocketTimeout int `mapstructure:"socket_timeout"`
}
type LoggingConfig struct {
Level string `mapstructure:"level"`
Format string `mapstructure:"format"`
DateFmt string `mapstructure:"datefmt"`
Retain int `mapstructure:"retain"`
}
type EncodingConfig struct {
FallbackCharset string `mapstructure:"fallback_charset"`
}
type PrivacyConfig struct {
EnableDelays bool `mapstructure:"enable_delays"`
EnableCoverTraffic bool `mapstructure:"enable_cover_traffic"`
StripAllMetadata bool `mapstructure:"strip_all_metadata"`
RandomizeOrder bool `mapstructure:"randomize_order"`
MultiHopRouting bool `mapstructure:"multi_hop_routing"`
}
type HeadersConfig struct {
StripFile string `mapstructure:"strip_file"`
StripList []string `mapstructure:"strip_list"`
AddNoArchive bool `mapstructure:"add_no_archive"`
AddGateway bool `mapstructure:"add_gateway_info"`
GatewayInfo string `mapstructure:"gateway_info"`
}
// Message ID Cache
type MessageIDCache struct {
cache map[string]time.Time
mu sync.RWMutex
maxAge time.Duration
cacheDir string
}
var (
config Config
messageCache *MessageIDCache
logFile *os.File
headersToStrip map[string]bool
)
// ============================================================================
// SECURE RANDOM FUNCTIONS
// ============================================================================
func SecureRandomInt(max int) int {
if max <= 0 {
return 0
}
n, err := rand.Int(rand.Reader, big.NewInt(int64(max)))
if err != nil {
return 0
}
return int(n.Int64())
}
func SecureRandom(size int) ([]byte, error) {
b := make([]byte, size)
_, err := rand.Read(b)
return b, err
}
func SecureRandomDelay() {
if !config.Privacy.EnableDelays {
return
}
delay := SecureRandomInt(MaxDelayMs-MinDelayMs) + MinDelayMs
jitter := SecureRandomInt(JitterMs*2) - JitterMs
totalDelay := delay + jitter
if totalDelay < 0 {
totalDelay = MinDelayMs
}
time.Sleep(time.Duration(totalDelay) * time.Millisecond)
}
func SecureZeroMemory(data []byte) {
for i := range data {
data[i] = 0
}
runtime.KeepAlive(data)
}
func SecureCompare(a, b string) bool {
return subtle.ConstantTimeCompare([]byte(a), []byte(b)) == 1
}
// ============================================================================
// MESSAGE ID CACHE
// ============================================================================
func NewMessageIDCache(maxAge time.Duration, cacheDir string) *MessageIDCache {
if cacheDir != "" {
os.MkdirAll(cacheDir, 0700)
}
cache := &MessageIDCache{
cache: make(map[string]time.Time),
maxAge: maxAge,
cacheDir: cacheDir,
}
cache.loadCache()
go cache.cleanupLoop()
return cache
}
func hashMessageID(mid string) string {
h := sha256.Sum256([]byte(mid))
return hex.EncodeToString(h[:])
}
func (c *MessageIDCache) Check(mid string) bool {
c.mu.RLock()
hash := hashMessageID(mid)
_, exists := c.cache[hash]
c.mu.RUnlock()
return exists
}
func (c *MessageIDCache) Add(mid string) {
c.mu.Lock()
hash := hashMessageID(mid)
c.cache[hash] = time.Now()
c.mu.Unlock()
c.saveCache()
}
func (c *MessageIDCache) loadCache() {
if c.cacheDir == "" {
return
}
cacheFile := filepath.Join(c.cacheDir, "msgid.cache")
data, err := os.ReadFile(cacheFile)
if err != nil {
return
}
c.mu.Lock()
defer c.mu.Unlock()
lines := strings.Split(string(data), "\n")
for _, line := range lines {
parts := strings.SplitN(line, "|", 2)
if len(parts) == 2 {
if t, err := time.Parse(time.RFC3339, parts[1]); err == nil {
if time.Since(t) < c.maxAge {
c.cache[parts[0]] = t
}
}
}
}
}
func (c *MessageIDCache) saveCache() {
if c.cacheDir == "" {
return
}
cacheFile := filepath.Join(c.cacheDir, "msgid.cache")
c.mu.RLock()
var lines []string
for hash, t := range c.cache {
lines = append(lines, fmt.Sprintf("%s|%s", hash, t.Format(time.RFC3339)))
}
c.mu.RUnlock()
os.WriteFile(cacheFile, []byte(strings.Join(lines, "\n")), 0600)
}
func (c *MessageIDCache) cleanupLoop() {
ticker := time.NewTicker(time.Hour)
for range ticker.C {
c.mu.Lock()
now := time.Now()
for hash, t := range c.cache {
if now.Sub(t) > c.maxAge {
delete(c.cache, hash)
}
}
c.mu.Unlock()
c.saveCache()
}
}
// ============================================================================
// HEADERS STRIPPING
// ============================================================================
func loadHeadersToStrip() {
headersToStrip = make(map[string]bool)
// Default headers to always strip for privacy
defaultStrip := []string{
"received",
"x-originating-ip",
"x-mailer",
"x-mimeole",
"x-msmail-priority",
"x-ms-has-attach",
"x-ms-tnef-correlator",
"x-auto-response-suppress",
"return-path",
"delivered-to",
"dkim-signature",
"domainkey-signature",
"arc-seal",
"arc-message-signature",
"arc-authentication-results",
"authentication-results",
"x-google-dkim-signature",
"x-gm-message-state",
"x-received",
"x-google-smtp-source",
}
for _, h := range defaultStrip {
headersToStrip[strings.ToLower(h)] = true
}
// Load from config strip_list
for _, h := range config.Headers.StripList {
headersToStrip[strings.ToLower(strings.TrimSpace(h))] = true
}
// Load from file if specified
stripFile := config.Headers.StripFile
if stripFile == "" {
stripFile = "/etc/mail2news/headers_strip"
}
if data, err := os.ReadFile(stripFile); err == nil {
lines := strings.Split(string(data), "\n")
for _, line := range lines {
line = strings.TrimSpace(line)
if line != "" && !strings.HasPrefix(line, "#") {
headersToStrip[strings.ToLower(line)] = true
}
}
logMessage("Loaded additional headers to strip from file", "INFO")
}
logMessage("Headers strip list loaded", "INFO")
}
func shouldStripHeader(headerName string) bool {
return headersToStrip[strings.ToLower(headerName)]
}
// ============================================================================
// LOGGING
// ============================================================================
func initLogging() {
logPath := config.Paths.Log
if logPath == "" {
logPath = "/var/log/mail2news/mail2news.log"
}
os.MkdirAll(filepath.Dir(logPath), 0700)
var err error
logFile, err = os.OpenFile(logPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
if err != nil {
log.Printf("Cannot open log file: %v", err)
return
}
log.SetOutput(io.MultiWriter(os.Stderr, logFile))
log.SetFlags(log.Ldate | log.Ltime)
}
func logMessage(msg, level string) {
// Sanitize log message to prevent injection
msg = strings.ReplaceAll(msg, "\n", " ")
msg = strings.ReplaceAll(msg, "\r", " ")
log.Printf("[%s] %s", level, msg)
}
// ============================================================================
// RECIPIENT PARSING
// ============================================================================
func parseRecipient(user string) (stamp string, newsgroups string, nospam bool) {
if idx := strings.Index(user, "@"); idx != -1 {
user = user[:idx]
}
// Classic format: mail2news[_nospam]-YYYYMMDD-newsgroup1=newsgroup2
re := regexp.MustCompile(`^(mail2news|mail2news_nospam)-([0-9]{8})-(.+)$`)
matches := re.FindStringSubmatch(user)
if matches == nil {
return "", "", false
}
nospam = (matches[1] == "mail2news_nospam")
stamp = matches[2]
newsgroups = strings.ReplaceAll(matches[3], "=", ",")
return stamp, newsgroups, nospam
}
func isSimpleRecipient(recipient string) (valid bool, nospam bool) {
local := recipient
if idx := strings.Index(recipient, "@"); idx != -1 {
local = recipient[:idx]
}
local = strings.ToLower(local)
if local == "mail2news" {
return true, false
}
if local == "mail2news_nospam" {
return true, true
}
return false, false
}
func validateStamp(stamp string) bool {
layout := "20060102"
parsedTime, err := time.Parse(layout, stamp)
if err != nil {
logMessage("Malformed date in recipient", "ERROR")
return false
}
now := time.Now().UTC()
beforeTime := now.Add(-time.Duration(config.Thresholds.HoursPast) * time.Hour)
afterTime := now.Add(time.Duration(config.Thresholds.HoursFuture) * time.Hour)
if parsedTime.After(beforeTime) && parsedTime.Before(afterTime) {
logMessage("Timestamp valid", "INFO")
return true
}
logMessage("Timestamp out of bounds", "WARNING")
return false
}
// ============================================================================
// NEWSGROUP VALIDATION
// ============================================================================
func ngvalidate(newsgroups string) string {
newsgroups = strings.TrimRight(newsgroups, ",")
groups := strings.Split(newsgroups, ",")
var goodng []string
re := regexp.MustCompile(`^[a-z][a-z0-9]+(\.[0-9a-z\-+_]+)+$`)
for _, ng := range groups {
ng = strings.TrimSpace(strings.ToLower(ng))
if re.MatchString(ng) {
isDuplicate := false
for _, existing := range goodng {
if existing == ng {
isDuplicate = true
break
}
}
if !isDuplicate {
goodng = append(goodng, ng)
}
}
}
if len(goodng) == 0 {
logMessage("No valid newsgroups found", "ERROR")
os.Exit(ExitReject)
}
if len(goodng) > config.Thresholds.MaxCrossposts {
logMessage("Crosspost limit exceeded", "ERROR")
os.Exit(ExitReject)
}
return strings.Join(goodng, ",")
}
// ============================================================================
// FROM PARSING
// ============================================================================
func fromParse(from string) (name, addr string) {
address, err := mail.ParseAddress(from)
if err != nil {
re := regexp.MustCompile(`<?([^\s<>]+@[^\s<>]+)>?`)
if matches := re.FindStringSubmatch(from); matches != nil {
return "", matches[1]
}
return from, ""
}
return address.Name, address.Address
}
func mungeFrom(from string) string {
name, addr := fromParse(from)
if addr == "" {
return from
}
obfuscated := strings.ReplaceAll(addr, "@", "<AT>")
obfuscated = strings.ReplaceAll(obfuscated, ".", "<DOT>")
if name != "" {
return fmt.Sprintf("%s <%s>", name, obfuscated)
}
return fmt.Sprintf("<%s>", obfuscated)
}
// ============================================================================
// MESSAGE PARSING - PRESERVE HEADERS
// ============================================================================
func msgParse(rawMessage string) (string, string) {
SecureRandomDelay()
msg, err := mail.ReadMessage(strings.NewReader(rawMessage))
if err != nil {
logMessage(fmt.Sprintf("Parse error: %v", err), "ERROR")
os.Exit(ExitError)
}
// Get recipient from available headers
recipient := ""
if to := msg.Header.Get("To"); to != "" {
recipient = to
}
if delivered := msg.Header.Get("Delivered-To"); delivered != "" {
if recipient == "" {
recipient = delivered
}
}
if xOrigTo := msg.Header.Get("X-Original-To"); xOrigTo != "" {
if recipient == "" {
recipient = xOrigTo
}
}
if recipient == "" {
logMessage("No recipient found", "ERROR")
os.Exit(ExitReject)
}
logMessage("Recipient accepted", "INFO")
// Check if recipient is for us
recipientLower := strings.ToLower(recipient)
if !strings.Contains(recipientLower, "mail2news") {
logMessage("Recipient not matching gateway prefix", "ERROR")
os.Exit(ExitReject)
}
// Determine format and get newsgroups
var newsgroups string
var nospam bool
if isSimple, isNospam := isSimpleRecipient(recipient); isSimple {
logMessage("Simple recipient format detected", "INFO")
nospam = isNospam
newsgroups = msg.Header.Get("Newsgroups")
if newsgroups == "" {
logMessage("Simple format requires Newsgroups: header", "ERROR")
os.Exit(ExitReject)
}
logMessage("Newsgroups found in header", "INFO")
} else {
if ng := msg.Header.Get("Newsgroups"); ng != "" {
newsgroups = ng
logMessage("Newsgroups found in header", "INFO")
nospam = strings.Contains(strings.ToLower(recipient), "nospam")
} else {
stamp, ng, ns := parseRecipient(recipient)
if stamp == "" || ng == "" {
logMessage("Invalid recipient format and no Newsgroups header", "ERROR")
os.Exit(ExitReject)
}
if !validateStamp(stamp) {
logMessage("Invalid timestamp", "ERROR")
os.Exit(ExitReject)
}
newsgroups = ng
nospam = ns
}
}
newsgroups = ngvalidate(newsgroups)
logMessage("Newsgroups validated", "INFO")
// Get or generate Message-ID
// SECURITY: Always regenerate Message-ID for anonymity (ignore client's)
// Format: standard Postfix-like pattern (timestamp.random@domain.invalid)
randomBytes, _ := SecureRandom(8)
messageID := fmt.Sprintf("<%d.%s@%s>",
time.Now().Unix(),
strings.ToUpper(hex.EncodeToString(randomBytes)),
config.NNTP.MessageIDDomain)
logMessage("Regenerated Message-ID for anonymity", "INFO")
// Check for duplicate
if messageCache.Check(messageID) {
logMessage("Duplicate message detected", "WARNING")
os.Exit(ExitDuplicate)
}
messageCache.Add(messageID)
// Read body
body, err := io.ReadAll(msg.Body)
if err != nil {
logMessage(fmt.Sprintf("Body read error: %v", err), "ERROR")
os.Exit(ExitError)
}
bodyStr := decodeBody(msg, body)
// ========================================================================
// BUILD HEADERS - Preserve original, strip unwanted, add required
// ========================================================================
var newHeaders strings.Builder
processedHeaders := make(map[string]bool)
// Process From header (with optional nospam munging)
from := msg.Header.Get("From")
if from == "" {
from = config.NNTP.DefaultFrom
}
if nospam {
from = mungeFrom(from)
logMessage("From address processed", "INFO")
}
newHeaders.WriteString(fmt.Sprintf("From: %s\r\n", from))
processedHeaders["from"] = true
// Newsgroups (validated)
newHeaders.WriteString(fmt.Sprintf("Newsgroups: %s\r\n", newsgroups))
processedHeaders["newsgroups"] = true
// Subject
subject := msg.Header.Get("Subject")
if subject == "" {
subject = "(no subject)"
}
newHeaders.WriteString(fmt.Sprintf("Subject: %s\r\n", subject))
processedHeaders["subject"] = true
// Date
date := msg.Header.Get("Date")
if date == "" {
date = time.Now().UTC().Format(time.RFC1123Z)
}
newHeaders.WriteString(fmt.Sprintf("Date: %s\r\n", date))
processedHeaders["date"] = true
// Message-ID
newHeaders.WriteString(fmt.Sprintf("Message-ID: %s\r\n", messageID))
processedHeaders["message-id"] = true
// Path
newHeaders.WriteString(fmt.Sprintf("Path: %s\r\n", config.NNTP.PathHeader))
processedHeaders["path"] = true
// Strip all recipient-related headers - prevent routing metadata leak
processedHeaders["x-original-to"] = true
processedHeaders["to"] = true
processedHeaders["delivered-to"] = true
// MIME headers - must reflect decoded body state
newHeaders.WriteString("MIME-Version: 1.0\r\n")
processedHeaders["mime-version"] = true
// Determine correct Content-Type after charset conversion
originalCT := msg.Header.Get("Content-Type")
if originalCT != "" {
mediaType, params, err := mime.ParseMediaType(originalCT)
if err == nil && strings.HasPrefix(mediaType, "text/") {
charset := ""
if c, ok := params["charset"]; ok {
charset = strings.ToLower(c)
}
// If charset was converted to UTF-8, update it
if charset == "iso-8859-1" || charset == "iso-8859-15" || charset == "windows-1252" || charset == "" {
newHeaders.WriteString("Content-Type: text/plain; charset=UTF-8\r\n")
} else if charset == "utf-8" || charset == "us-ascii" {
newHeaders.WriteString(fmt.Sprintf("Content-Type: %s; charset=%s\r\n", mediaType, charset))
} else {
newHeaders.WriteString("Content-Type: text/plain; charset=UTF-8\r\n")
}
} else {
// Non-text content type, preserve as-is
newHeaders.WriteString(fmt.Sprintf("Content-Type: %s\r\n", originalCT))
}
} else {
newHeaders.WriteString("Content-Type: text/plain; charset=UTF-8\r\n")
}
processedHeaders["content-type"] = true
// Body was decoded from base64/qp, so transfer encoding is now 8bit
newHeaders.WriteString("Content-Transfer-Encoding: 8bit\r\n")
processedHeaders["content-transfer-encoding"] = true
// X-No-Archive
if config.Headers.AddNoArchive {
if existing := msg.Header.Get("X-No-Archive"); existing == "" {
newHeaders.WriteString("X-No-Archive: Yes\r\n")
}
}
processedHeaders["x-no-archive"] = true
// X-Gateway-Info
if config.Headers.AddGateway {
gatewayInfo := config.Headers.GatewayInfo
if gatewayInfo == "" {
gatewayInfo = "mail2news"
}
newHeaders.WriteString(fmt.Sprintf("X-Gateway-Info: %s\r\n", gatewayInfo))
}
// Now process ALL remaining original headers, preserving them unless stripped
// Get all header keys and sort them for consistent output
var headerKeys []string
for key := range msg.Header {
headerKeys = append(headerKeys, key)
}
sort.Strings(headerKeys)
for _, key := range headerKeys {
keyLower := strings.ToLower(key)
// Skip if already processed
if processedHeaders[keyLower] {
continue
}
// Skip if in strip list
if shouldStripHeader(key) {
continue
}
// Preserve this header
values := msg.Header[key]
for _, value := range values {
newHeaders.WriteString(fmt.Sprintf("%s: %s\r\n", key, value))
}
processedHeaders[keyLower] = true
}
logMessage("Headers processed", "INFO")
// End headers
newHeaders.WriteString("\r\n")
// Combine headers and body
fullMessage := newHeaders.String() + bodyStr
return messageID, fullMessage
}
func decodeBody(msg *mail.Message, body []byte) string {
contentType := msg.Header.Get("Content-Type")
encoding := msg.Header.Get("Content-Transfer-Encoding")
var decoded []byte
switch strings.ToLower(encoding) {
case "quoted-printable":
reader := quotedprintable.NewReader(bytes.NewReader(body))
decoded, _ = io.ReadAll(reader)
case "base64":
decoded, _ = base64.StdEncoding.DecodeString(string(body))
default:
decoded = body
}
// Handle charset conversion
if contentType != "" {
mediaType, params, err := mime.ParseMediaType(contentType)
if err == nil && strings.HasPrefix(mediaType, "text/") {
if charset, ok := params["charset"]; ok {
charset = strings.ToLower(charset)
if charset == "iso-8859-1" || charset == "iso-8859-15" || charset == "windows-1252" {
decoder := charmap.ISO8859_1.NewDecoder()
result, err := decoder.Bytes(decoded)
if err == nil {
decoded = result
}
}
}
}
}
return string(decoded)
}
// ============================================================================
// NNTP SENDING
// ============================================================================
func newsSend(messageID, payload string) {
SecureRandomDelay()
servers := make([]string, len(config.NNTP.OnionServers))
copy(servers, config.NNTP.OnionServers)
if config.Privacy.RandomizeOrder && len(servers) > 1 {
for i := len(servers) - 1; i > 0; i-- {
j := SecureRandomInt(i + 1)
servers[i], servers[j] = servers[j], servers[i]
}
}
for i, server := range servers {
if err := postToServer(server, payload, true); err == nil {
logMessage(fmt.Sprintf("Posted successfully (server %d)", i+1), "INFO")
return
} else {
logMessage(fmt.Sprintf("Server %d failed", i+1), "WARNING")
}
}
// Try clearnet fallback if configured
if len(config.NNTP.ClearnetServers) > 0 && !config.NNTP.AlwaysUseTor {
for i, server := range config.NNTP.ClearnetServers {
if err := postToServer(server, payload, false); err == nil {
logMessage(fmt.Sprintf("Posted successfully (clearnet %d)", i+1), "INFO")
return
} else {
logMessage(fmt.Sprintf("Clearnet server %d failed", i+1), "WARNING")
}
}
}
logMessage("All servers failed", "ERROR")
os.Exit(ExitError)
}
func postToServer(server, payload string, useTor bool) error {
var conn net.Conn
var err error
timeout := time.Duration(config.Thresholds.SocketTimeout) * time.Second
if timeout == 0 {
timeout = 60 * time.Second
}
if strings.Contains(server, ".onion") || config.NNTP.AlwaysUseTor || useTor {
torProxy := config.NNTP.TorProxy
if torProxy == "" {
torProxy = "127.0.0.1:9050"
}
dialer, err := proxy.SOCKS5("tcp", torProxy, nil, proxy.Direct)
if err != nil {
return fmt.Errorf("tor proxy error: %w", err)
}
conn, err = dialer.Dial("tcp", server)
if err != nil {
return fmt.Errorf("tor connect error: %w", err)
}
} else {
conn, err = net.DialTimeout("tcp", server, timeout)
if err != nil {
return fmt.Errorf("connect error: %w", err)
}
}
defer conn.Close()
conn.SetDeadline(time.Now().Add(timeout))
reader := bufio.NewReader(conn)
tp := textproto.NewReader(reader)
// Read greeting
code, _, err := tp.ReadCodeLine(200)
if err != nil || code != 200 {
return fmt.Errorf("greeting error: %d %v", code, err)
}
// MODE READER
fmt.Fprintf(conn, "MODE READER\r\n")
tp.ReadCodeLine(200)
// POST
fmt.Fprintf(conn, "POST\r\n")
code, _, err = tp.ReadCodeLine(340)
if err != nil || code != 340 {
return fmt.Errorf("POST rejected: %d %v", code, err)
}
// Send message
lines := strings.Split(payload, "\n")
for _, line := range lines {
line = strings.TrimRight(line, "\r")
if strings.HasPrefix(line, ".") {
line = "." + line
}
fmt.Fprintf(conn, "%s\r\n", line)
}
fmt.Fprintf(conn, ".\r\n")
// Read response
code, nntpMsg, err := tp.ReadCodeLine(240)
if err != nil || code != 240 {
return fmt.Errorf("post failed: %d %s %v", code, nntpMsg, err)
}
// QUIT
fmt.Fprintf(conn, "QUIT\r\n")
return nil
}
// ============================================================================
// MAIN
// ============================================================================
func main() {
// Load config
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath("/etc/mail2news")
viper.AddConfigPath(".")
if err := viper.ReadInConfig(); err != nil {
log.Fatalf("Config error: %s", err)
}
if err := viper.Unmarshal(&config); err != nil {
log.Fatalf("Config parse error: %s", err)
}
// Set defaults
if config.Paths.Log == "" {
config.Paths.Log = "/var/log/mail2news/mail2news.log"
}
if config.Thresholds.MaxCrossposts == 0 {
config.Thresholds.MaxCrossposts = 3
}
if config.Thresholds.HoursPast == 0 {
config.Thresholds.HoursPast = 48
}
if config.Thresholds.HoursFuture == 0 {
config.Thresholds.HoursFuture = 24
}
if config.NNTP.PathHeader == "" {
config.NNTP.PathHeader = "not-for-mail"
}
if config.NNTP.MessageIDDomain == "" {
config.NNTP.MessageIDDomain = "mail2news.local"
}
if config.NNTP.DefaultFrom == "" {
config.NNTP.DefaultFrom = "Anonymous <nobody@anonymous.invalid>"
}
// Enable privacy features by default
if !config.Privacy.EnableDelays {
config.Privacy.EnableDelays = true
config.Privacy.RandomizeOrder = true
}
// Enable gateway headers by default
if !config.Headers.AddNoArchive {
config.Headers.AddNoArchive = true
}
if !config.Headers.AddGateway {
config.Headers.AddGateway = true
}
initLogging()
loadHeadersToStrip()
messageCache = NewMessageIDCache(CacheExpiration, "/var/lib/mail2news/cache")
logMessage("Gateway initialized", "INFO")
// Read message from stdin
message, err := io.ReadAll(os.Stdin)
if err != nil {
logMessage("Input error", "ERROR")
os.Exit(ExitError)
}
if len(message) > config.Thresholds.MaxBytes && config.Thresholds.MaxBytes > 0 {
logMessage("Message exceeds size limit", "ERROR")
os.Exit(ExitReject)
}
mid, payload := msgParse(string(message))
newsSend(mid, payload)
// Secure cleanup
SecureZeroMemory(message)
logMessage("Completed successfully", "INFO")
}
|