]> Cypherpunks.ru repositories - gocheese.git/blob - ucspi.go
More convenient trusted-host
[gocheese.git] / ucspi.go
1 // GoCheese -- Python private package repository and caching proxy
2 // Copyright (C) 2019-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 main
17
18 import (
19         "io"
20         "net"
21         "net/http"
22         "os"
23         "sync"
24         "time"
25 )
26
27 var (
28         ALongTimeAgo = time.Unix(1, 0)
29         UCSPIJob     sync.WaitGroup
30 )
31
32 type UCSPIAddr struct {
33         ip   string
34         port string
35 }
36
37 func (addr *UCSPIAddr) Network() string { return "tcp" }
38
39 func (addr *UCSPIAddr) String() string { return addr.ip + ":" + addr.port }
40
41 type UCSPIConn struct {
42         eof chan struct{}
43 }
44
45 type ReadResult struct {
46         n   int
47         err error
48 }
49
50 func (conn *UCSPIConn) Read(b []byte) (int, error) {
51         c := make(chan ReadResult)
52         go func() {
53                 n, err := os.Stdin.Read(b)
54                 c <- ReadResult{n, err}
55         }()
56         select {
57         case res := <-c:
58                 return res.n, res.err
59         case <-conn.eof:
60                 return 0, io.EOF
61         }
62 }
63
64 func (conn *UCSPIConn) Write(b []byte) (int, error) { return os.Stdout.Write(b) }
65
66 func (conn *UCSPIConn) Close() error {
67         if err := os.Stdin.Close(); err != nil {
68                 return err
69         }
70         return os.Stdout.Close()
71 }
72
73 func (conn *UCSPIConn) LocalAddr() net.Addr {
74         return &UCSPIAddr{ip: os.Getenv("TCPLOCALIP"), port: os.Getenv("TCPLOCALPORT")}
75 }
76
77 func (conn *UCSPIConn) RemoteAddr() net.Addr {
78         return &UCSPIAddr{ip: os.Getenv("TCPREMOTEIP"), port: os.Getenv("TCPREMOTEPORT")}
79 }
80
81 func (conn *UCSPIConn) SetDeadline(t time.Time) error {
82         if err := os.Stdin.SetReadDeadline(t); err != nil {
83                 return err
84         }
85         return os.Stdout.SetWriteDeadline(t)
86 }
87
88 func (conn *UCSPIConn) SetReadDeadline(t time.Time) error {
89         // An ugly hack to forcefully terminate pending read.
90         // net/http calls SetReadDeadline(aLongTimeAgo), but file
91         // descriptors are not capable to exit immediately that way.
92         if t.Equal(ALongTimeAgo) {
93                 conn.eof <- struct{}{}
94         }
95         return os.Stdin.SetReadDeadline(t)
96 }
97
98 func (conn *UCSPIConn) SetWriteDeadline(t time.Time) error {
99         return os.Stdout.SetWriteDeadline(t)
100 }
101
102 type UCSPI struct{ accepted bool }
103
104 type UCSPIAlreadyAccepted struct{}
105
106 func (u UCSPIAlreadyAccepted) Error() string {
107         return "already accepted"
108 }
109
110 func (ucspi *UCSPI) Accept() (net.Conn, error) {
111         if ucspi.accepted {
112                 return nil, UCSPIAlreadyAccepted{}
113         }
114         ucspi.accepted = true
115         conn := UCSPIConn{eof: make(chan struct{}, 1)}
116         return &conn, nil
117 }
118
119 func (ucspi *UCSPI) Close() error {
120         return nil
121 }
122
123 func (ucspi *UCSPI) Addr() net.Addr {
124         return &UCSPIAddr{ip: os.Getenv("TCPLOCALIP"), port: os.Getenv("TCPLOCALPORT")}
125 }
126
127 func connStater(conn net.Conn, connState http.ConnState) {
128         switch connState {
129         case http.StateNew:
130                 UCSPIJob.Add(1)
131         case http.StateClosed:
132                 UCSPIJob.Done()
133         }
134 }