Skip to content

Commit

Permalink
fix http download for binary
Browse files Browse the repository at this point in the history
  • Loading branch information
Son Roy Almerol committed Dec 19, 2024
1 parent f8a74da commit 4afb1d7
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 20 deletions.
21 changes: 16 additions & 5 deletions cmd/windows_agent/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (p *agentService) Start(s service.Service) error {
func (p *agentService) startPing() {
ping := func() {
var pingResp PingResp
pingErr := agent.ProxmoxHTTPRequest(http.MethodGet, "/api2/json/ping", nil, &pingResp)
_, pingErr := agent.ProxmoxHTTPRequest(http.MethodGet, "/api2/json/ping", nil, &pingResp)
if pingErr != nil {
agent.SetStatus(fmt.Sprintf("Error - (%s)", pingErr.Error()))
} else if !pingResp.Data.Pong {
Expand Down Expand Up @@ -94,7 +94,7 @@ func (p *agentService) versionCheck() {
}

versionCheck := func() {
_ = agent.ProxmoxHTTPRequest(http.MethodGet, "/api2/json/plus/version", nil, &versionResp)
_, _ = agent.ProxmoxHTTPRequest(http.MethodGet, "/api2/json/plus/version", nil, &versionResp)
}

versionCheck()
Expand All @@ -105,20 +105,26 @@ func (p *agentService) versionCheck() {
return
case <-time.After(time.Minute * 2):
if versionResp.Version != Version {
var dlResp io.Reader
err := agent.ProxmoxHTTPRequest(http.MethodGet, "/api2/json/plus/binary", dlResp, nil)
var dlResp io.ReadCloser
dlResp, err := agent.ProxmoxHTTPRequest(http.MethodGet, "/api2/json/plus/binary", nil, nil)
if err != nil {
if hasLogger {
logger.Errorf("Update download %s error: %s", versionResp.Version, err)
}
continue
}

closeResp := func() {
_, _ = io.Copy(io.Discard, dlResp)
dlResp.Close()
}

err = selfupdate.Apply(dlResp, selfupdate.Options{})
if err != nil {
if hasLogger {
logger.Errorf("Update download %s error: %s", versionResp.Version, err)
}
closeResp()
continue
}

Expand All @@ -127,6 +133,7 @@ func (p *agentService) versionCheck() {
if hasLogger {
logger.Errorf("Update download %s error: %s", versionResp.Version, err)
}
closeResp()
continue
}

Expand Down Expand Up @@ -187,16 +194,20 @@ func (p *agentService) run() {
return
}

err = agent.ProxmoxHTTPRequest(
body, err := agent.ProxmoxHTTPRequest(
http.MethodPost,
"/api2/json/d2d/target/agent",
bytes.NewBuffer(reqBody),
nil,
)

if err != nil {
logger.Errorf("Agent drives update error: %v", err)
return
}

_, _ = io.Copy(io.Discard, body)
body.Close()
}()

for _, drive := range drives {
Expand Down
2 changes: 1 addition & 1 deletion internal/agent/cache/exclusions.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type ExclusionResp struct {

func CompileExcludedPaths() []*regexp.Regexp {
var exclusionResp ExclusionResp
err := agent.ProxmoxHTTPRequest(
_, err := agent.ProxmoxHTTPRequest(
http.MethodGet,
"/api2/json/d2d/exclusion",
nil,
Expand Down
2 changes: 1 addition & 1 deletion internal/agent/cache/partial_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var PartialFilePathRegexes = CompilePartialFileList()

func CompilePartialFileList() []*regexp.Regexp {
var partialResp PartialFileResp
err := agent.ProxmoxHTTPRequest(
_, err := agent.ProxmoxHTTPRequest(
http.MethodGet,
"/api2/json/d2d/partial-file",
nil,
Expand Down
27 changes: 15 additions & 12 deletions internal/agent/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ import (

var httpClient *http.Client

func ProxmoxHTTPRequest(method, url string, body io.Reader, respBody any) error {
func ProxmoxHTTPRequest(method, url string, body io.Reader, respBody any) (io.ReadCloser, error) {
serverUrl := ""
key, err := registry.OpenKey(registry.LOCAL_MACHINE, `Software\PBSPlus\Config`, registry.QUERY_VALUE)
if err != nil {
return fmt.Errorf("ProxmoxHTTPRequest: server url not found -> %w", err)
return nil, fmt.Errorf("ProxmoxHTTPRequest: server url not found -> %w", err)
}
defer key.Close()

Expand All @@ -42,7 +42,7 @@ func ProxmoxHTTPRequest(method, url string, body io.Reader, respBody any) error
}

if serverUrl, _, err = key.GetStringValue("ServerURL"); err != nil || serverUrl == "" {
return fmt.Errorf("ProxmoxHTTPRequest: server url not found -> %w", err)
return nil, fmt.Errorf("ProxmoxHTTPRequest: server url not found -> %w", err)
}

req, err := http.NewRequest(
Expand All @@ -56,7 +56,7 @@ func ProxmoxHTTPRequest(method, url string, body io.Reader, respBody any) error
)

if err != nil {
return fmt.Errorf("ProxmoxHTTPRequest: error creating http request -> %w", err)
return nil, fmt.Errorf("ProxmoxHTTPRequest: error creating http request -> %w", err)
}

hostname, _ := os.Hostname()
Expand All @@ -76,24 +76,27 @@ func ProxmoxHTTPRequest(method, url string, body io.Reader, respBody any) error

resp, err := httpClient.Do(req)
if err != nil {
return fmt.Errorf("ProxmoxHTTPRequest: error executing http request -> %w", err)
return nil, fmt.Errorf("ProxmoxHTTPRequest: error executing http request -> %w", err)
}

if respBody == nil {
return resp.Body, nil
}

defer func() {
_, _ = io.Copy(io.Discard, resp.Body)
resp.Body.Close()
}()

rawBody, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("ProxmoxHTTPRequest: error getting body content -> %w", err)
return nil, fmt.Errorf("ProxmoxHTTPRequest: error getting body content -> %w", err)
}

if respBody != nil {
err = json.Unmarshal(rawBody, respBody)
if err != nil {
return fmt.Errorf("ProxmoxHTTPRequest: error json unmarshal body content (%s) -> %w", string(rawBody), err)
}
err = json.Unmarshal(rawBody, respBody)
if err != nil {
return nil, fmt.Errorf("ProxmoxHTTPRequest: error json unmarshal body content (%s) -> %w", string(rawBody), err)
}

return nil
return nil, nil
}
7 changes: 6 additions & 1 deletion internal/syslog/windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
Expand Down Expand Up @@ -46,12 +47,16 @@ func (l *Logger) logToServer(level string, msg string) {
return
}

_ = agent.ProxmoxHTTPRequest(
body, err := agent.ProxmoxHTTPRequest(
http.MethodPost,
"/api2/json/d2d/agent-log",
bytes.NewBuffer(reqBody),
nil,
)
if err == nil {
_, _ = io.Copy(io.Discard, body)
body.Close()
}
}

func (l *Logger) Infof(format string, v ...any) {
Expand Down

0 comments on commit 4afb1d7

Please sign in to comment.