A working proxy exists.
This commit is contained in:
79
proxy/main.go
Normal file
79
proxy/main.go
Normal file
@@ -0,0 +1,79 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"ippserver/packages/ipp"
|
||||
"ippserver/packages/mdnsserver"
|
||||
"net/http"
|
||||
"sync"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func main() {
|
||||
customFormatter := new(log.TextFormatter)
|
||||
customFormatter.TimestampFormat = "2006-01-02 15:04:05"
|
||||
log.SetFormatter(customFormatter)
|
||||
customFormatter.FullTimestamp = true
|
||||
log.SetLevel(log.InfoLevel)
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
go mdnsserver.Run(ctx)
|
||||
|
||||
http.HandleFunc("/ipp/print", handle)
|
||||
|
||||
log.Info("http server started on :1234")
|
||||
err := http.ListenAndServe(":1234", nil)
|
||||
if err != nil {
|
||||
log.Fatal("ListenAndServe: " + err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
var mut sync.Mutex
|
||||
var requestID uint32
|
||||
|
||||
func handle(w http.ResponseWriter, r *http.Request) {
|
||||
mut.Lock()
|
||||
requestID++
|
||||
mut.Unlock()
|
||||
if r.Method != http.MethodPost {
|
||||
http.Error(w, "Unsupported method", http.StatusMethodNotAllowed)
|
||||
|
||||
}
|
||||
//log.Info(r.Header)
|
||||
//body := make([]byte, r.ContentLength)
|
||||
//io.ReadFull(r.Body, body)
|
||||
//log.Infof("Body %x", body)
|
||||
|
||||
request := ipp.NewRequest(0, 0)
|
||||
//rdata := make([]byte, r.ContentLength)
|
||||
//io.ReadFull(r.Body, rdata)
|
||||
//log.Printf("Data %x", rdata)
|
||||
//fmt.Printf("data % #0x", rdata)
|
||||
//buf := bytes.NewBuffer(rdata)
|
||||
request.UnMarshal(r.Body)
|
||||
log.Infof("Upstream Request: \n%v\n", request)
|
||||
var response *ipp.Response
|
||||
switch request.Operation() {
|
||||
case ipp.GetPrinterAttributes:
|
||||
response = handleGetPrinterAttributes(request)
|
||||
case ipp.PrintJob:
|
||||
response = handlePrintJob(request, r.Body, requestID)
|
||||
case ipp.GetJobs:
|
||||
response = handleGetJobs(request, requestID)
|
||||
case ipp.ValidateJob:
|
||||
response = handleValidateJob(request)
|
||||
case ipp.GetJobAttributes:
|
||||
response = handleGetJobAttributes(request, requestID)
|
||||
default:
|
||||
response = ipp.NewResponse(ipp.ClientErrorBadRequest, request.RequestID())
|
||||
}
|
||||
|
||||
log.Infof("Upstream Response:\n%v\n", response)
|
||||
data := response.Marshal()
|
||||
|
||||
//log.Debugf("% x", data)
|
||||
w.Write(data)
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user