diff --git a/go.mod b/go.mod index daa31238..497b9dac 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module rpc go 1.20 // uncomment if developing with go-wsman-messages locally -replace github.com/open-amt-cloud-toolkit/go-wsman-messages/v2 => ../go-wsman-messages +// replace github.com/open-amt-cloud-toolkit/go-wsman-messages/v2 => ../go-wsman-messages require ( github.com/google/uuid v1.6.0 diff --git a/internal/local/amt/localTransport.go b/internal/local/amt/localTransport.go index 595832ca..a0da25df 100644 --- a/internal/local/amt/localTransport.go +++ b/internal/local/amt/localTransport.go @@ -64,7 +64,6 @@ func (l *LocalTransport) RoundTrip(r *http.Request) (*http.Response, error) { // wait for channel open confirmation l.waitGroup.Wait() logrus.Trace("Channel open confirmation received") - // Serialize the HTTP request to raw form rawRequest, err := serializeHTTPRequest(r) if err != nil { @@ -73,8 +72,8 @@ func (l *LocalTransport) RoundTrip(r *http.Request) (*http.Response, error) { } var responseReader *bufio.Reader - // send our data to LMX - err = l.local.Send(rawRequest) + + err = l.local.Send([]byte(rawRequest)) if err != nil { logrus.Error(err) return nil, err @@ -111,6 +110,8 @@ Loop: func serializeHTTPRequest(r *http.Request) ([]byte, error) { var reqBuffer bytes.Buffer + r.Header.Set("Transfer-Encoding", "chunked") + // Write request line reqLine := fmt.Sprintf("%s %s %s\r\n", r.Method, r.URL.RequestURI(), r.Proto) reqBuffer.WriteString(reqLine) @@ -125,8 +126,12 @@ func serializeHTTPRequest(r *http.Request) ([]byte, error) { if err != nil { return nil, err } + length := fmt.Sprintf("%x", len(bodyBytes)) + bodyBytes = append([]byte(length+"\r\n"), bodyBytes...) + bodyBytes = append(bodyBytes, []byte("\r\n0\r\n\r\n")...) // Important: Replace the body so it can be read again later if needed r.Body = io.NopCloser(bytes.NewBuffer(bodyBytes)) + reqBuffer.Write(bodyBytes) } diff --git a/internal/local/amt/wsman.go b/internal/local/amt/wsman.go index 71e28703..f05a23cc 100644 --- a/internal/local/amt/wsman.go +++ b/internal/local/amt/wsman.go @@ -7,6 +7,7 @@ package amt import ( "encoding/base64" + "net" "rpc/pkg/utils" "github.com/open-amt-cloud-toolkit/go-wsman-messages/v2/pkg/wsman" @@ -29,6 +30,7 @@ import ( "github.com/open-amt-cloud-toolkit/go-wsman-messages/v2/pkg/wsman/ips/hostbasedsetup" "github.com/open-amt-cloud-toolkit/go-wsman-messages/v2/pkg/wsman/ips/ieee8021x" "github.com/open-amt-cloud-toolkit/go-wsman-messages/v2/pkg/wsman/ips/optin" + "github.com/sirupsen/logrus" ) type WSMANer interface { @@ -94,7 +96,6 @@ func NewGoWSMANMessages(lmsAddress string) *GoWSMANMessages { } func (g *GoWSMANMessages) SetupWsmanClient(username string, password string, logAMTMessages bool) { - clientParams := client.Parameters{ Target: g.target, Username: username, @@ -102,11 +103,21 @@ func (g *GoWSMANMessages) SetupWsmanClient(username string, password string, log UseDigest: true, UseTLS: false, LogAMTMessages: logAMTMessages, - Transport: NewLocalTransport(), } - + logrus.Info("Attempting to connect to LMS...") + port := utils.LMSPort + if clientParams.UseTLS { + port = client.TLSPort + } + con, err := net.Dial("tcp4", utils.LMSAddress+":"+port) + if err != nil { + logrus.Info("Failed to connect to LMS, using local transport instead.") + clientParams.Transport = NewLocalTransport() + } else { + logrus.Info("Successfully connected to LMS.") + con.Close() + } g.wsmanMessages = wsman.NewMessages(clientParams) - } func (g *GoWSMANMessages) GetGeneralSettings() (general.Response, error) {