github.com/piotrnar/gocoin@v0.0.0-20240512203912-faa0448c5e96/client/usif/webui/config.go (about)

     1  package webui
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"net/http"
     8  	"strconv"
     9  	"time"
    10  
    11  	"github.com/piotrnar/gocoin/client/common"
    12  	"github.com/piotrnar/gocoin/client/network"
    13  	"github.com/piotrnar/gocoin/client/network/peersdb"
    14  	"github.com/piotrnar/gocoin/client/usif"
    15  	"github.com/piotrnar/gocoin/client/wallet"
    16  	"github.com/piotrnar/gocoin/lib/btc"
    17  	"github.com/piotrnar/gocoin/lib/others/sys"
    18  )
    19  
    20  func p_cfg(w http.ResponseWriter, r *http.Request) {
    21  	if !ipchecker(r) {
    22  		return
    23  	}
    24  
    25  	if common.CFG.WebUI.ServerMode {
    26  		return
    27  	}
    28  
    29  	if r.Method == "POST" {
    30  		if len(r.Form["configjson"]) > 0 {
    31  			common.LockCfg()
    32  			e := json.Unmarshal([]byte(r.Form["configjson"][0]), &common.CFG)
    33  			if e == nil {
    34  				common.Reset()
    35  			}
    36  			if len(r.Form["save"]) > 0 {
    37  				common.SaveConfig()
    38  			}
    39  			common.UnlockCfg()
    40  			http.Redirect(w, r, "/", http.StatusFound)
    41  			return
    42  		}
    43  
    44  		if len(r.Form["friends_file"]) > 0 {
    45  			ioutil.WriteFile(common.GocoinHomeDir+"friends.txt", []byte(r.Form["friends_file"][0]), 0600)
    46  			network.Mutex_net.Lock()
    47  			network.NextConnectFriends = time.Now()
    48  			network.Mutex_net.Unlock()
    49  			http.Redirect(w, r, "/net", http.StatusFound)
    50  			return
    51  		}
    52  
    53  		if len(r.Form["shutdown"]) > 0 {
    54  			usif.Exit_now.Set()
    55  			w.Write([]byte("Your node should shut down soon"))
    56  			return
    57  		}
    58  
    59  		if len(r.Form["wallet"]) > 0 {
    60  			if r.Form["wallet"][0] == "on" {
    61  				wallet.OnOff <- true
    62  			} else if r.Form["wallet"][0] == "off" {
    63  				wallet.OnOff <- false
    64  			}
    65  			if len(r.Form["page"]) > 0 {
    66  				http.Redirect(w, r, r.Form["page"][0], http.StatusFound)
    67  			} else {
    68  				http.Redirect(w, r, "/wal", http.StatusFound)
    69  			}
    70  			return
    71  		}
    72  
    73  		return
    74  	}
    75  
    76  	// for any other GET we need a matching session-id
    77  	if !checksid(r) {
    78  		new_session_id(w)
    79  		return
    80  	}
    81  
    82  	if len(r.Form["getmp"]) > 0 {
    83  		if conid, e := strconv.ParseUint(r.Form["getmp"][0], 10, 32); e == nil {
    84  			network.GetMP(uint32(conid))
    85  		}
    86  		return
    87  	}
    88  
    89  	if len(r.Form["freemem"]) > 0 {
    90  		sys.FreeMem()
    91  		http.Redirect(w, r, "/", http.StatusFound)
    92  		return
    93  	}
    94  
    95  	if len(r.Form["drop"]) > 0 {
    96  		if conid, e := strconv.ParseUint(r.Form["drop"][0], 10, 32); e == nil {
    97  			network.DropPeer(uint32(conid))
    98  		}
    99  		http.Redirect(w, r, "net", http.StatusFound)
   100  		return
   101  	}
   102  
   103  	if len(r.Form["conn"]) > 0 {
   104  		ad, er := peersdb.NewAddrFromString(r.Form["conn"][0], false)
   105  		if er != nil {
   106  			w.Write([]byte(er.Error()))
   107  			return
   108  		}
   109  		w.Write([]byte(fmt.Sprint("Connecting to ", ad.Ip())))
   110  		ad.Manual = true
   111  		network.DoNetwork(ad)
   112  		return
   113  	}
   114  
   115  	if len(r.Form["unban"]) > 0 {
   116  		w.Write([]byte(usif.UnbanPeer(r.Form["unban"][0])))
   117  		return
   118  	}
   119  
   120  	// All the functions below change modify the config file
   121  	common.LockCfg()
   122  	defer common.UnlockCfg()
   123  
   124  	if len(r.Form["txponoff"]) > 0 {
   125  		common.CFG.TXPool.Enabled = !common.CFG.TXPool.Enabled
   126  		http.Redirect(w, r, "txs", http.StatusFound)
   127  		return
   128  	}
   129  
   130  	if len(r.Form["txronoff"]) > 0 {
   131  		common.CFG.TXRoute.Enabled = !common.CFG.TXRoute.Enabled
   132  		http.Redirect(w, r, "txs", http.StatusFound)
   133  		return
   134  	}
   135  
   136  	if len(r.Form["lonoff"]) > 0 {
   137  		common.CFG.Net.ListenTCP = !common.CFG.Net.ListenTCP
   138  		common.ListenTCP = common.CFG.Net.ListenTCP
   139  		http.Redirect(w, r, "net", http.StatusFound)
   140  		return
   141  	}
   142  
   143  	if len(r.Form["savecfg"]) > 0 {
   144  		dat, _ := json.MarshalIndent(&common.CFG, "", "    ")
   145  		if dat != nil {
   146  			ioutil.WriteFile(common.ConfigFile, dat, 0660)
   147  		}
   148  		http.Redirect(w, r, "/", http.StatusFound)
   149  		return
   150  	}
   151  
   152  	if len(r.Form["trusthash"]) > 0 {
   153  		if btc.NewUint256FromString(r.Form["trusthash"][0]) != nil {
   154  			common.CFG.LastTrustedBlock = r.Form["trusthash"][0]
   155  			common.ApplyLastTrustedBlock()
   156  		}
   157  		w.Write([]byte(common.CFG.LastTrustedBlock))
   158  		return
   159  	}
   160  }