]> Cypherpunks.ru repositories - gocheese.git/blob - ucspi.go
More convenient trusted-host
[gocheese.git] / ucspi.go
1 /*
2 GoCheese -- Python private package repository and caching proxy
3 Copyright (C) 2019-2022 Sergey Matveev <stargrave@stargrave.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, version 3 of the License.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 package main
19
20 import (
21         "io"
22         "net"
23         "net/http"
24         "os"
25         "sync"
26         "time"
27 )
28
29 var (
30         ALongTimeAgo = time.Unix(1, 0)
31         UCSPIJob     sync.WaitGroup
32 )
33
34 type UCSPIAddr struct {
35         ip   string
36         port string
37 }
38
39 func (addr *UCSPIAddr) Network() string { return "tcp" }
40
41 func (addr *UCSPIAddr) String() string { return addr.ip + ":" + addr.port }
42
43 type UCSPIConn struct {
44         eof chan struct{}
45 }
46
47 type ReadResult struct {
48         n   int
49         err error
50 }
51
52 func (conn *UCSPIConn) Read(b []byte) (int, error) {
53         c := make(chan ReadResult)
54         go func() {
55                 n, err := os.Stdin.Read(b)
56                 c <- ReadResult{n, err}
57         }()
58         select {
59         case res := <-c:
60                 return res.n, res.err
61         case <-conn.eof:
62                 return 0, io.EOF
63         }
64 }
65
66 func (conn *UCSPIConn) Write(b []byte) (int, error) { return os.Stdout.Write(b) }
67
68 func (conn *UCSPIConn) Close() error {
69         if err := os.Stdin.Close(); err != nil {
70                 return err
71         }
72         return os.Stdout.Close()
73 }
74
75 func (conn *UCSPIConn) LocalAddr() net.Addr {
76         return &UCSPIAddr{ip: os.Getenv("TCPLOCALIP"), port: os.Getenv("TCPLOCALPORT")}
77 }
78
79 func (conn *UCSPIConn) RemoteAddr() net.Addr {
80         return &UCSPIAddr{ip: os.Getenv("TCPREMOTEIP"), port: os.Getenv("TCPREMOTEPORT")}
81 }
82
83 func (conn *UCSPIConn) SetDeadline(t time.Time) error {
84         if err := os.Stdin.SetReadDeadline(t); err != nil {
85                 return err
86         }
87         return os.Stdout.SetWriteDeadline(t)
88 }
89
90 func (conn *UCSPIConn) SetReadDeadline(t time.Time) error {
91         // An ugly hack to forcefully terminate pending read.
92         // net/http calls SetReadDeadline(aLongTimeAgo), but file
93         // descriptors are not capable to exit immediately that way.
94         if t.Equal(ALongTimeAgo) {
95                 conn.eof <- struct{}{}
96         }
97         return os.Stdin.SetReadDeadline(t)
98 }
99
100 func (conn *UCSPIConn) SetWriteDeadline(t time.Time) error {
101         return os.Stdout.SetWriteDeadline(t)
102 }
103
104 type UCSPI struct{ accepted bool }
105
106 type UCSPIAlreadyAccepted struct{}
107
108 func (u UCSPIAlreadyAccepted) Error() string {
109         return "already accepted"
110 }
111
112 func (ucspi *UCSPI) Accept() (net.Conn, error) {
113         if ucspi.accepted {
114                 return nil, UCSPIAlreadyAccepted{}
115         }
116         ucspi.accepted = true
117         conn := UCSPIConn{eof: make(chan struct{}, 1)}
118         return &conn, nil
119 }
120
121 func (ucspi *UCSPI) Close() error {
122         return nil
123 }
124
125 func (ucspi *UCSPI) Addr() net.Addr {
126         return &UCSPIAddr{ip: os.Getenv("TCPLOCALIP"), port: os.Getenv("TCPLOCALPORT")}
127 }
128
129 func connStater(conn net.Conn, connState http.ConnState) {
130         switch connState {
131         case http.StateNew:
132                 UCSPIJob.Add(1)
133         case http.StateClosed:
134                 UCSPIJob.Done()
135         }
136 }