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