]> Sergey Matveev's repositories - uploader.git/blobdiff - ucspi.go
UCSPI-TCP support
[uploader.git] / ucspi.go
diff --git a/ucspi.go b/ucspi.go
new file mode 100644 (file)
index 0000000..74c5b79
--- /dev/null
+++ b/ucspi.go
@@ -0,0 +1,103 @@
+/*
+uploader -- simplest form file uploader
+Copyright (C) 2018-2021 Sergey Matveev <stargrave@stargrave.org>
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, version 3 of the License.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+package main
+
+import (
+       "net"
+       "net/http"
+       "os"
+       "time"
+)
+
+type UCSPIAddr struct {
+       ip   string
+       port string
+}
+
+func (addr *UCSPIAddr) Network() string { return "tcp" }
+
+func (addr *UCSPIAddr) String() string { return addr.ip + ":" + addr.port }
+
+type UCSPIConn struct{}
+
+func (conn *UCSPIConn) Read(b []byte) (int, error) { return os.Stdin.Read(b) }
+
+func (conn *UCSPIConn) Write(b []byte) (int, error) { return os.Stdout.Write(b) }
+
+func (conn *UCSPIConn) Close() error {
+       if err := os.Stdin.Close(); err != nil {
+               return err
+       }
+       return os.Stdin.Close()
+}
+
+func (conn *UCSPIConn) LocalAddr() net.Addr {
+       return &UCSPIAddr{ip: os.Getenv("TCPLOCALIP"), port: os.Getenv("TCPLOCALPORT")}
+}
+
+func (conn *UCSPIConn) RemoteAddr() net.Addr {
+       return &UCSPIAddr{ip: os.Getenv("TCPREMOTEIP"), port: os.Getenv("TCPREMOTEPORT")}
+}
+
+func (conn *UCSPIConn) SetDeadline(t time.Time) error {
+       if err := os.Stdin.SetReadDeadline(t); err != nil {
+               return err
+       }
+       return os.Stdout.SetWriteDeadline(t)
+}
+
+func (conn *UCSPIConn) SetReadDeadline(t time.Time) error {
+       return os.Stdin.SetReadDeadline(t)
+}
+
+func (conn *UCSPIConn) SetWriteDeadline(t time.Time) error {
+       return os.Stdout.SetWriteDeadline(t)
+}
+
+type UCSPI struct{ accepted bool }
+
+type UCSPIAlreadyAccepted struct{}
+
+func (u UCSPIAlreadyAccepted) Error() string {
+       return "already accepted"
+}
+
+func (ucspi *UCSPI) Accept() (net.Conn, error) {
+       if ucspi.accepted {
+               return nil, UCSPIAlreadyAccepted{}
+       }
+       ucspi.accepted = true
+       return &UCSPIConn{}, nil
+}
+
+func (ucspi *UCSPI) Close() error {
+       return nil
+}
+
+func (ucspi *UCSPI) Addr() net.Addr {
+       return &UCSPIAddr{ip: os.Getenv("TCPLOCALIP"), port: os.Getenv("TCPLOCALPORT")}
+}
+
+func connStater(conn net.Conn, connState http.ConnState) {
+       switch connState {
+       case http.StateNew:
+               Jobs.Add(1)
+       case http.StateClosed:
+               Jobs.Done()
+       }
+}