Skip to content

Commit

Permalink
feat: enable local config commands without lms
Browse files Browse the repository at this point in the history
  • Loading branch information
rsdmike committed Jun 17, 2024
1 parent d7564a5 commit ec45f29
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 8 additions & 3 deletions internal/local/amt/localTransport.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}

Expand Down
19 changes: 15 additions & 4 deletions internal/local/amt/wsman.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand Down Expand Up @@ -94,19 +96,28 @@ func NewGoWSMANMessages(lmsAddress string) *GoWSMANMessages {
}

func (g *GoWSMANMessages) SetupWsmanClient(username string, password string, logAMTMessages bool) {

clientParams := client.Parameters{
Target: g.target,
Username: username,
Password: password,
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) {
Expand Down

0 comments on commit ec45f29

Please sign in to comment.