]> Sergey Matveev's repositories - uploader.git/blob - ucspi.go
UCSPI-TCP support
[uploader.git] / ucspi.go
1 /*
2 uploader -- simplest form file uploader
3 Copyright (C) 2018-2021 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         "net"
22         "net/http"
23         "os"
24         "time"
25 )
26
27 type UCSPIAddr struct {
28         ip   string
29         port string
30 }
31
32 func (addr *UCSPIAddr) Network() string { return "tcp" }
33
34 func (addr *UCSPIAddr) String() string { return addr.ip + ":" + addr.port }
35
36 type UCSPIConn struct{}
37
38 func (conn *UCSPIConn) Read(b []byte) (int, error) { return os.Stdin.Read(b) }
39
40 func (conn *UCSPIConn) Write(b []byte) (int, error) { return os.Stdout.Write(b) }
41
42 func (conn *UCSPIConn) Close() error {
43         if err := os.Stdin.Close(); err != nil {
44                 return err
45         }
46         return os.Stdin.Close()
47 }
48
49 func (conn *UCSPIConn) LocalAddr() net.Addr {
50         return &UCSPIAddr{ip: os.Getenv("TCPLOCALIP"), port: os.Getenv("TCPLOCALPORT")}
51 }
52
53 func (conn *UCSPIConn) RemoteAddr() net.Addr {
54         return &UCSPIAddr{ip: os.Getenv("TCPREMOTEIP"), port: os.Getenv("TCPREMOTEPORT")}
55 }
56
57 func (conn *UCSPIConn) SetDeadline(t time.Time) error {
58         if err := os.Stdin.SetReadDeadline(t); err != nil {
59                 return err
60         }
61         return os.Stdout.SetWriteDeadline(t)
62 }
63
64 func (conn *UCSPIConn) SetReadDeadline(t time.Time) error {
65         return os.Stdin.SetReadDeadline(t)
66 }
67
68 func (conn *UCSPIConn) SetWriteDeadline(t time.Time) error {
69         return os.Stdout.SetWriteDeadline(t)
70 }
71
72 type UCSPI struct{ accepted bool }
73
74 type UCSPIAlreadyAccepted struct{}
75
76 func (u UCSPIAlreadyAccepted) Error() string {
77         return "already accepted"
78 }
79
80 func (ucspi *UCSPI) Accept() (net.Conn, error) {
81         if ucspi.accepted {
82                 return nil, UCSPIAlreadyAccepted{}
83         }
84         ucspi.accepted = true
85         return &UCSPIConn{}, nil
86 }
87
88 func (ucspi *UCSPI) Close() error {
89         return nil
90 }
91
92 func (ucspi *UCSPI) Addr() net.Addr {
93         return &UCSPIAddr{ip: os.Getenv("TCPLOCALIP"), port: os.Getenv("TCPLOCALPORT")}
94 }
95
96 func connStater(conn net.Conn, connState http.ConnState) {
97         switch connState {
98         case http.StateNew:
99                 Jobs.Add(1)
100         case http.StateClosed:
101                 Jobs.Done()
102         }
103 }