Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
K
koko
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ops
koko
Commits
7d67f134
Commit
7d67f134
authored
Apr 29, 2019
by
ibuler
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[Update] 修改命令解析
parent
870b9366
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
81 additions
and
20 deletions
+81
-20
hello.go
cmd/hello.go
+1
-0
parser.go
pkg/proxy/parser.go
+75
-9
proxy.go
pkg/proxy/proxy.go
+5
-11
terminal.go
pkg/utils/terminal.go
+0
-0
No files found.
cmd/hello.go
0 → 100644
View file @
7d67f134
package
main
pkg/proxy/parser.go
View file @
7d67f134
...
@@ -3,10 +3,13 @@ package proxy
...
@@ -3,10 +3,13 @@ package proxy
import
(
import
(
"bytes"
"bytes"
"fmt"
"fmt"
"os"
"strings"
"sync"
"sync"
"cocogo/pkg/logger"
"cocogo/pkg/logger"
"cocogo/pkg/model"
"cocogo/pkg/model"
"cocogo/pkg/utils"
)
)
type
ParseRule
func
([]
byte
)
bool
type
ParseRule
func
([]
byte
)
bool
...
@@ -26,15 +29,34 @@ var (
...
@@ -26,15 +29,34 @@ var (
charEnter
=
[]
byte
(
"
\r
"
)
charEnter
=
[]
byte
(
"
\r
"
)
)
)
type
CmdParser
struct
{
term
*
utils
.
Terminal
buf
*
bytes
.
Buffer
}
func
(
cp
*
CmdParser
)
Reset
()
{
cp
.
buf
.
Reset
()
}
func
(
cp
*
CmdParser
)
Initial
()
{
cp
.
buf
=
new
(
bytes
.
Buffer
)
cp
.
term
=
utils
.
NewTerminal
(
cp
.
buf
,
""
)
cp
.
term
.
SetEcho
(
false
)
}
func
(
cp
*
CmdParser
)
Parse
(
b
[]
byte
)
string
{
cp
.
buf
.
Write
(
b
)
cp
.
buf
.
WriteString
(
"
\r
"
)
lines
,
_
:=
cp
.
term
.
ReadLines
()
return
strings
.
TrimSpace
(
strings
.
Join
(
lines
,
"
\r\n
"
))
}
// Parse 解析用户输入输出, 拦截过滤用户输入输出
// Parse 解析用户输入输出, 拦截过滤用户输入输出
type
Parser
struct
{
type
Parser
struct
{
inputBuf
*
bytes
.
Buffer
inputBuf
*
bytes
.
Buffer
cmdBuf
*
bytes
.
Buffer
cmdBuf
*
bytes
.
Buffer
outputBuf
*
bytes
.
Buffer
outputBuf
*
bytes
.
Buffer
userInputChan
chan
[]
byte
serverInputChan
chan
[]
byte
filterRules
[]
model
.
SystemUserFilterRule
filterRules
[]
model
.
SystemUserFilterRule
inputInitial
bool
inputInitial
bool
...
@@ -44,6 +66,25 @@ type Parser struct {
...
@@ -44,6 +66,25 @@ type Parser struct {
zmodemState
string
zmodemState
string
inVimState
bool
inVimState
bool
once
sync
.
Once
once
sync
.
Once
command
string
output
string
cmdInputParser
*
CmdParser
cmdOutputParser
*
CmdParser
counter
int
}
func
(
p
*
Parser
)
Initial
()
{
p
.
inputBuf
=
new
(
bytes
.
Buffer
)
p
.
cmdBuf
=
new
(
bytes
.
Buffer
)
p
.
outputBuf
=
new
(
bytes
.
Buffer
)
p
.
once
=
sync
.
Once
{}
p
.
cmdInputParser
=
&
CmdParser
{}
p
.
cmdOutputParser
=
&
CmdParser
{}
p
.
cmdInputParser
.
Initial
()
p
.
cmdOutputParser
.
Initial
()
}
}
// Todo: parseMultipleInput 依然存在问题
// Todo: parseMultipleInput 依然存在问题
...
@@ -56,17 +97,41 @@ func (p *Parser) parseInputState(b []byte) {
...
@@ -56,17 +97,41 @@ func (p *Parser) parseInputState(b []byte) {
p
.
inputPreState
=
p
.
inputState
p
.
inputPreState
=
p
.
inputState
if
bytes
.
Contains
(
b
,
charEnter
)
{
if
bytes
.
Contains
(
b
,
charEnter
)
{
p
.
inputState
=
false
p
.
inputState
=
false
//fmt.Printf("Command: %s\n", p.inputBuf.String())
p
.
parseCmdInput
()
p
.
inputBuf
.
Reset
()
}
else
{
}
else
{
p
.
inputState
=
true
p
.
inputState
=
true
if
!
p
.
inputPreState
{
if
!
p
.
inputPreState
{
//fmt.Printf("Output: %s\n", p.outputBuf.String())
p
.
parseCmdOutput
()
p
.
outputBuf
.
Reset
()
}
}
}
}
}
}
var
f
,
_
=
os
.
Create
(
"/tmp/cmd.text"
)
func
(
p
*
Parser
)
parseCmdInput
()
{
parser
:=
CmdParser
{}
parser
.
Initial
()
data
:=
p
.
cmdBuf
.
Bytes
()
fmt
.
Printf
(
"原始输入: %b
\n
"
,
data
)
line
:=
parser
.
Parse
(
data
)
data2
:=
fmt
.
Sprintf
(
"[%d] 命令: %s
\n
"
,
p
.
counter
,
line
)
fmt
.
Printf
(
data2
)
p
.
cmdBuf
.
Reset
()
p
.
inputBuf
.
Reset
()
f
.
WriteString
(
data2
)
}
func
(
p
*
Parser
)
parseCmdOutput
()
{
data
:=
p
.
outputBuf
.
Bytes
()
line
:=
p
.
cmdOutputParser
.
Parse
(
data
)
data2
:=
fmt
.
Sprintf
(
"[%d] 结果: %s
\n
"
,
p
.
counter
,
line
)
_
=
fmt
.
Sprintf
(
"[%d] 结果: %s
\n
"
,
p
.
counter
,
line
)
fmt
.
Printf
(
data2
)
p
.
outputBuf
.
Reset
()
f
.
WriteString
(
data2
)
p
.
counter
+=
1
}
func
(
p
*
Parser
)
parseInputNewLine
(
b
[]
byte
)
[]
byte
{
func
(
p
*
Parser
)
parseInputNewLine
(
b
[]
byte
)
[]
byte
{
b
=
bytes
.
Replace
(
b
,
[]
byte
{
'\r'
,
'\r'
,
'\n'
},
[]
byte
{
'\r'
},
-
1
)
b
=
bytes
.
Replace
(
b
,
[]
byte
{
'\r'
,
'\r'
,
'\n'
},
[]
byte
{
'\r'
},
-
1
)
b
=
bytes
.
Replace
(
b
,
[]
byte
{
'\r'
,
'\n'
},
[]
byte
{
'\r'
},
-
1
)
b
=
bytes
.
Replace
(
b
,
[]
byte
{
'\r'
,
'\n'
},
[]
byte
{
'\r'
},
-
1
)
...
@@ -80,7 +145,6 @@ func (p *Parser) ParseUserInput(b []byte) []byte {
...
@@ -80,7 +145,6 @@ func (p *Parser) ParseUserInput(b []byte) []byte {
})
})
nb
:=
p
.
parseInputNewLine
(
b
)
nb
:=
p
.
parseInputNewLine
(
b
)
p
.
inputBuf
.
Write
(
nb
)
p
.
inputBuf
.
Write
(
nb
)
fmt
.
Printf
(
"User input: %b
\n
"
,
b
)
p
.
parseInputState
(
nb
)
p
.
parseInputState
(
nb
)
return
b
return
b
}
}
...
@@ -117,6 +181,9 @@ func (p *Parser) parseZmodemState(b []byte) {
...
@@ -117,6 +181,9 @@ func (p *Parser) parseZmodemState(b []byte) {
}
}
func
(
p
*
Parser
)
parseCommand
(
b
[]
byte
)
{
func
(
p
*
Parser
)
parseCommand
(
b
[]
byte
)
{
if
!
p
.
inputInitial
{
return
}
if
p
.
inputState
{
if
p
.
inputState
{
p
.
cmdBuf
.
Write
(
b
)
p
.
cmdBuf
.
Write
(
b
)
}
else
{
}
else
{
...
@@ -128,6 +195,5 @@ func (p *Parser) ParseServerOutput(b []byte) []byte {
...
@@ -128,6 +195,5 @@ func (p *Parser) ParseServerOutput(b []byte) []byte {
p
.
parseVimState
(
b
)
p
.
parseVimState
(
b
)
p
.
parseZmodemState
(
b
)
p
.
parseZmodemState
(
b
)
p
.
parseCommand
(
b
)
p
.
parseCommand
(
b
)
fmt
.
Printf
(
"Server output: %s
\n
"
,
b
)
return
b
return
b
}
}
pkg/proxy/proxy.go
View file @
7d67f134
package
proxy
package
proxy
import
(
import
(
"bytes"
"context"
"context"
"sync"
"github.com/ibuler/ssh"
"github.com/ibuler/ssh"
"cocogo/pkg/logger"
"cocogo/pkg/logger"
...
@@ -72,17 +69,14 @@ func (p *ProxyServer) Proxy(ctx context.Context) {
...
@@ -72,17 +69,14 @@ func (p *ProxyServer) Proxy(ctx context.Context) {
if
err
!=
nil
{
if
err
!=
nil
{
logger
.
Error
(
"Get system user filter rule error: "
,
err
)
logger
.
Error
(
"Get system user filter rule error: "
,
err
)
}
}
parser
:=
&
Parser
{
filterRules
:
rules
,
}
parser
.
Initial
()
sw
:=
Switch
{
sw
:=
Switch
{
userSession
:
p
.
Session
,
userSession
:
p
.
Session
,
serverConn
:
&
conn
,
serverConn
:
&
conn
,
parser
:
&
Parser
{
parser
:
parser
,
once
:
sync
.
Once
{},
userInputChan
:
make
(
chan
[]
byte
,
5
),
inputBuf
:
new
(
bytes
.
Buffer
),
outputBuf
:
new
(
bytes
.
Buffer
),
cmdBuf
:
new
(
bytes
.
Buffer
),
filterRules
:
rules
,
},
}
}
sw
.
Bridge
(
ctx
)
sw
.
Bridge
(
ctx
)
}
}
pkg/utils/terminal.go
0 → 100644
View file @
7d67f134
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment