-
Notifications
You must be signed in to change notification settings - Fork 197
/
conn_sqlserver.go
133 lines (119 loc) · 3.17 KB
/
conn_sqlserver.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
package srvconn
import (
"bytes"
"fmt"
"os"
"strconv"
_ "github.com/denisenkom/go-mssqldb"
"github.com/jumpserver/koko/pkg/localcommand"
"github.com/jumpserver/koko/pkg/logger"
)
const (
sqlServerPrompt = "Password:"
)
var (
_ ServerConnection = (*SQLServerConn)(nil)
)
func NewSQLServerConnection(ops ...SqlOption) (*SQLServerConn, error) {
args := &sqlOption{
Username: os.Getenv("USER"),
Password: os.Getenv("PASSWORD"),
Host: "127.0.0.1",
Port: 1433,
DBName: "",
win: Windows{
Width: 80,
Height: 120,
},
}
for _, setter := range ops {
setter(args)
}
if err := checkSQLServerAccount(args); err != nil {
return nil, err
}
lCmd, err := startSQLServerCommand(args)
if err != nil {
return nil, err
}
err = lCmd.SetWinSize(args.win.Width, args.win.Height)
if err != nil {
_ = lCmd.Close()
return nil, err
}
return &SQLServerConn{options: args, LocalCommand: lCmd}, nil
}
type SQLServerConn struct {
options *sqlOption
*localcommand.LocalCommand
}
func (conn *SQLServerConn) KeepAlive() error {
return nil
}
func (conn *SQLServerConn) Close() error {
_, _ = conn.Write(cleanLineExitCommand)
return conn.LocalCommand.Close()
}
func startSQLServerCommand(opt *sqlOption) (lcmd *localcommand.LocalCommand, err error) {
if lcmd, err = startSQLServerNormalCommand(opt); err != nil {
return nil, err
}
return tryManualLoginSQLServerServer(opt, lcmd)
}
func startSQLServerNormalCommand(opt *sqlOption) (lcmd *localcommand.LocalCommand, err error) {
//tsql 是启动sqlserver的客户端
opts, err := BuildNobodyWithOpts(localcommand.WithPtyWin(opt.win.Width, opt.win.Height))
if err != nil {
logger.Errorf("build nobody with opts error: %s", err)
return nil, err
}
return localcommand.New("tsql", opt.SQLServerCommandArgs(), opts...)
}
func tryManualLoginSQLServerServer(opt *sqlOption, lcmd *localcommand.LocalCommand) (*localcommand.LocalCommand, error) {
var (
nr int
err error
)
prompt := [len(sqlServerPrompt)]byte{}
nr, err = lcmd.Read(prompt[:])
if err != nil {
_ = lcmd.Close()
logger.Errorf("sqlserver local pty fd read err: %s", err)
return lcmd, err
}
if !bytes.Equal(prompt[:nr], []byte(sqlServerPrompt)) {
_ = lcmd.Close()
logger.Errorf("sqlserver login prompt characters did not match: %s", prompt[:nr])
err = fmt.Errorf("sqlserver login prompt characters did not match: %s", prompt[:nr])
return lcmd, err
}
// 输入密码, 登录 sqlserver
_, err = lcmd.Write([]byte(opt.Password + "\r\n"))
if err != nil {
_ = lcmd.Close()
logger.Errorf("sqlserver local pty write err: %s", err)
return lcmd, fmt.Errorf("sqlserver conn err: %s", err)
}
return lcmd, nil
}
func (opt *sqlOption) SQLServerCommandArgs() []string {
return []string{
"-U", opt.Username,
"-H", opt.Host,
"-p", strconv.Itoa(opt.Port),
"-J", "UTF-8",
"-D", opt.DBName,
}
}
func (opt *sqlOption) SQLServerSourceName() string {
return fmt.Sprintf("server=%s;port=%s;database=%s;user id=%s;password=%s;encrypt=disable",
opt.Host,
strconv.Itoa(opt.Port),
opt.DBName,
opt.Username,
opt.Password,
)
}
func checkSQLServerAccount(args *sqlOption) error {
return checkDatabaseAccountValidate("mssql", args.SQLServerSourceName())
}