This commit is contained in:
crusader 2018-10-23 15:02:07 +09:00
parent 59add71a72
commit c72e7b5a0a

View File

@ -138,16 +138,23 @@ func hadlePrePacket(matchCtx *osm.MatchCtx, _connector connector, conn net.Conn,
} }
buf := make([]byte, 1024) buf := make([]byte, 1024)
var discoveredMatcher osm.Matcher var (
discoveredMatcher osm.Matcher
packetCount int
_packet *osm.Packet
err error
nRead int
)
LOOP: LOOP:
for _, _matcher := range matchers { for _, _matcher := range matchers {
matchCtx.InitAttribute() matchCtx.InitAttribute()
if err := _matcher.Match(matchCtx, 0, packet); nil != err { err = _matcher.Match(matchCtx, 0, packet)
if nil != err {
continue LOOP continue LOOP
} }
packetCount := _matcher.PacketCount(matchCtx) packetCount = _matcher.PacketCount(matchCtx)
if 0 == packetCount { if 0 == packetCount {
return _matcher return _matcher
@ -155,25 +162,28 @@ LOOP:
INNER_LOOP: INNER_LOOP:
for j := 0; j < packetCount; j++ { for j := 0; j < packetCount; j++ {
_packet := _matcher.Packet(matchCtx, j) _packet = _matcher.Packet(matchCtx, j)
if err := conn.SetWriteDeadline(time.Now().Add(deadline)); nil != err { err = conn.SetWriteDeadline(time.Now().Add(deadline))
if nil != err {
return nil return nil
} }
_, err := conn.Write(_packet.Buffer) _, err = conn.Write(_packet.Buffer)
if nil != err { if nil != err {
return nil return nil
} }
if err := conn.SetReadDeadline(time.Now().Add(deadline)); nil != err { err = conn.SetReadDeadline(time.Now().Add(deadline))
if nil != err {
return nil return nil
} }
n, err := conn.Read(buf) nRead, err = conn.Read(buf)
if nil != err { if nil != err {
return nil return nil
} }
if err := _matcher.Match(matchCtx, j+1, osm.NewPacket(buf, n)); nil == err { err = _matcher.Match(matchCtx, j+1, osm.NewPacket(buf, nRead))
if nil == err {
discoveredMatcher = _matcher discoveredMatcher = _matcher
} else { } else {
discoveredMatcher = nil discoveredMatcher = nil
@ -202,49 +212,57 @@ func hadlePostPacket(matchCtx *osm.MatchCtx, _connector connector, targetPort *o
} }
buf := make([]byte, 1024) buf := make([]byte, 1024)
var discoveredMatcher osm.Matcher var (
discoveredMatcher osm.Matcher
packetCount int
_packet *osm.Packet
err error
conn net.Conn
nRead int
)
LOOP: LOOP:
for _, _matcher := range matchers { for _, _matcher := range matchers {
matchCtx.InitAttribute() matchCtx.InitAttribute()
packetCount := _matcher.PacketCount(matchCtx) packetCount = _matcher.PacketCount(matchCtx)
if 0 == packetCount { if 0 == packetCount {
continue LOOP continue LOOP
} }
conn, err := _connector.dial(targetPort) conn, err = _connector.dial(targetPort)
if nil != err { if nil != err {
return nil return nil
} }
INNER_LOOP: INNER_LOOP:
for j := 0; j < packetCount; j++ { for j := 0; j < packetCount; j++ {
_packet := _matcher.Packet(matchCtx, j) _packet = _matcher.Packet(matchCtx, j)
if err := conn.SetWriteDeadline(time.Now().Add(deadline)); nil != err { err = conn.SetWriteDeadline(time.Now().Add(deadline))
break INNER_LOOP if nil != err {
} break INNER_LOOP
_, err := conn.Write(_packet.Buffer) }
_, err = conn.Write(_packet.Buffer)
if nil != err { if nil != err {
log.Print(err)
break INNER_LOOP break INNER_LOOP
} }
if err := conn.SetReadDeadline(time.Now().Add(deadline)); nil != err { err = conn.SetReadDeadline(time.Now().Add(deadline))
if nil != err {
break INNER_LOOP break INNER_LOOP
} }
n, err := conn.Read(buf) nRead, err = conn.Read(buf)
if nil != err { if nil != err {
// if !_matcher.HasResponse(matchCtx, j) { // if !_matcher.HasResponse(matchCtx, j) {
// discoveredMatcher = _matcher // discoveredMatcher = _matcher
// } // }
break INNER_LOOP break INNER_LOOP
} }
// log.Printf("res: %s", string(buf[:n])) // log.Printf("res: %s", string(buf[:n]))
if err := _matcher.Match(matchCtx, j, osm.NewPacket(buf, n)); err == nil { err = _matcher.Match(matchCtx, j, osm.NewPacket(buf, nRead))
if err == nil {
if packetCount-1 == j { if packetCount-1 == j {
discoveredMatcher = _matcher discoveredMatcher = _matcher