]> Cypherpunks.ru repositories - udpobfs.git/blob - utils.go
Unify copyright comment format
[udpobfs.git] / utils.go
1 // udpobfs -- simple point-to-point UDP obfuscation proxy
2 // Copyright (C) 2023-2024 Sergey Matveev <stargrave@stargrave.org>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, version 3 of the License.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16 package udpobfs
17
18 import (
19         "io"
20         "log"
21         "net"
22         "time"
23 )
24
25 const BufLen = 1 << 14
26
27 var (
28         PingDuration     = 10 * time.Second
29         LifetimeDuration = time.Minute
30 )
31
32 func MustWrite(w io.Writer, data []byte) {
33         if n, err := w.Write(data); err != nil || n != len(data) {
34                 log.Fatal("non full write")
35         }
36 }
37
38 func Incr(buf []byte) (overflow bool) {
39         for i := len(buf) - 1; i >= 0; i-- {
40                 buf[i]++
41                 if buf[i] != 0 {
42                         return
43                 }
44         }
45         overflow = true
46         return
47 }
48
49 func MustResolveUDPAddr(addr string) *net.UDPAddr {
50         a, err := net.ResolveUDPAddr("udp", addr)
51         if err != nil {
52                 log.Fatal(err)
53         }
54         return a
55 }
56
57 func MustResolveTCPAddr(addr string) *net.TCPAddr {
58         a, err := net.ResolveTCPAddr("tcp", addr)
59         if err != nil {
60                 log.Fatal(err)
61         }
62         return a
63 }
64
65 type Buf struct {
66         Buf *[BufLen]byte
67         N   int
68 }