forked from racingmars/proxy3270
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
194 lines (166 loc) · 5.73 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
* Copyright 2020 by Matthew R. Wilson <mwilson@mattwilson.org>
*
* This file is part of proxy3270.
*
* proxy3270 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, either version 3 of the License, or
* (at your option) any later version.
*
* proxy3270 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 proxy3270. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import (
"flag"
"fmt"
"net"
"os"
"os/signal"
"strconv"
"time"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/racingmars/go3270"
)
var config []ServerConfig
var screen go3270.Screen
var rules go3270.Rules
func main() {
var err error
debug := flag.Bool("debug", false, "sets log level to debug")
debug3270 := flag.Bool("debug3270", false, "enables debugging in the go3270 library")
trace := flag.Bool("trace", false, "sets log level to trace")
port := flag.Int("port", 3270, "port number to listen on")
configFile := flag.String("config", "config.json", "configuration file path")
telnetTimeout := flag.Int("telnetTimeout", 1, "length of time to wait for telnet command response from clients when un-negotiating the 3270 session")
logFile := flag.String("log", "", "log file name to enable logging to a file")
flag.Parse()
consoleWriter := zerolog.ConsoleWriter{Out: os.Stdout /*, TimeFormat: "02 15:04:05"*/}
log.Logger = log.Output(consoleWriter)
zerolog.SetGlobalLevel(zerolog.InfoLevel)
if *trace {
zerolog.SetGlobalLevel(zerolog.TraceLevel)
} else if *debug {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
}
if *logFile != "" {
f, err := os.OpenFile(*logFile,
os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0660)
if err != nil {
log.Error().Err(err).Msg("Couldn't open log file")
return
}
defer f.Close()
multi := zerolog.MultiLevelWriter(consoleWriter, f)
log.Logger = zerolog.New(multi).With().Timestamp().Logger()
log.Info().Msgf("Logging to file %s", *logFile)
}
if *debug3270 {
go3270.Debug = os.Stderr
}
if *telnetTimeout < 1 {
log.Error().Err(err).Msg("telnetTimeout must be positive")
return
}
config, err = loadConfig(*configFile)
if err != nil {
log.Error().Err(err).Msg("Couldn't load config file")
return
}
err = validateConfig(config)
if err != nil {
log.Error().Err(err).Msg("Config error")
return
}
screen, rules = buildScreen(config)
ln, err := net.Listen("tcp", ":"+strconv.Itoa(*port))
if err != nil {
log.Error().Err(err).Msg("Couldn't start listener")
return
}
log.Info().Msgf("LISTENING ON PORT %d FOR CONNECTIONS", *port)
log.Info().Msg("Press Ctrl-C to end server.")
// Run the accept loop in a goroutine so we can wait on the quit signal
go func() {
for {
conn, err := ln.Accept()
if err != nil {
log.Error().Err(err).Msg("Couldn't accept connection")
}
log.Info().Msgf("New connection from %s", conn.RemoteAddr())
go handle(conn, *telnetTimeout)
}
}()
quit := make(chan os.Signal)
signal.Notify(quit, os.Interrupt)
<-quit
log.Info().Msg("Interrupt signal received: quitting.")
return
}
func handle(conn net.Conn, timeout int) {
defer conn.Close()
if err := go3270.NegotiateTelnet(conn); err != nil {
log.Error().Err(err).Msgf("couldn't negotiate connection from %s", conn.RemoteAddr())
return
}
response, err := go3270.HandleScreen(screen, rules, nil,
[]go3270.AID{go3270.AIDEnter}, []go3270.AID{go3270.AIDPF3},
"errormsg", 2, 33, conn)
if err != nil {
log.Error().Err(err).Msgf("err: couldn't handle screen for %s", conn.RemoteAddr())
return
}
if response.AID == go3270.AIDPF3 {
return
}
selection, _ := strconv.Atoi(response.Values["input"])
selection = selection - 1
remote := fmt.Sprintf("%s:%d", config[selection].Host, config[selection].Port)
if err = go3270.UnNegotiateTelnet(conn, time.Second*time.Duration(timeout)); err != nil {
log.Error().Err(err).Msgf("Couldn't unnegotiate client")
return
}
log.Info().Msgf("Connecting client %s to server %s", conn.RemoteAddr(), remote)
if err = proxy(conn, remote); err != nil {
log.Error().Err(err).Msgf("Error proxying to %s", remote)
}
log.Info().Msgf("Client %s session ended", conn.RemoteAddr())
}
func buildScreen(config []ServerConfig) (go3270.Screen, go3270.Rules) {
screen := make(go3270.Screen, 0)
rules := make(go3270.Rules)
screen = append(screen, go3270.Field{Row: 0, Col: 27, Intense: true, Content: "3270 Proxy Application"})
screen = append(screen, go3270.Field{Row: 2, Col: 2, Content: "Select service to connect to:"})
screen = append(screen, go3270.Field{Row: 2, Col: 32, Name: "input", Highlighting: go3270.Underscore, Write: true})
screen = append(screen, go3270.Field{Row: 2, Col: 35}) // Field "stop" character
screen = append(screen, go3270.Field{Row: 20, Col: 0, Intense: true, Color: go3270.Red, Name: "errormsg"})
screen = append(screen, go3270.Field{Row: 22, Col: 0, Content: "PF3 Exit"})
for i := range config {
var rowBase = 4
var colBase = 2
// Wrap to the second column
if i > 12 {
rowBase = -9
colBase = 41
}
screen = append(screen, go3270.Field{Row: rowBase + i, Col: colBase, Content: fmt.Sprintf("%2d", i+1), Intense: true})
screen = append(screen, go3270.Field{Row: rowBase + i, Col: colBase + 3, Content: config[i].Name})
}
v := func(input string) bool {
if val, err := strconv.Atoi(input); err != nil {
return false
} else if val < 1 || val > len(config) {
return false
}
return true
}
rules["input"] = go3270.FieldRules{Validator: v}
return screen, rules
}