forked from mihailescu2m/powerwall_monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(api): fixes for tesla 20.49 API changes
With the 20.49 firmware update by Tesla, the endpoints that previously were open are no longer without authentication. Unfortuantely, the mechanism used for authentication is not HTTP basic, rather it's cookie based. This brings in a very simple cookie aware proxy to run in the telegraf container. In short, cron updates the cookies every two minutes and then cookieproxy forwards to connection with cookies through to the Powerwall. Fixes mihailescu2m#14 DCO 1.1 Signed-off-by: Patrick Wagstrom <[email protected]>
- Loading branch information
Showing
6 changed files
with
103 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
FROM golang:1.16-buster as build | ||
RUN go get github.com/pridkett/cookieproxy | ||
|
||
FROM telegraf:1.17 | ||
|
||
RUN apt-get update | ||
RUN apt-get install -y cron | ||
|
||
COPY --from=build /go/bin/cookieproxy . | ||
COPY cookieproxy.sh /etc/init.d/cookieproxy | ||
COPY entrypoint.sh / |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#!/bin/sh | ||
# Start/stop cookieproxy | ||
# | ||
### BEGIN INIT INFO | ||
# Provides: cron | ||
# Required-Start: $network | ||
# Required-Stop: $network | ||
# Default-Start: 2 3 4 5 | ||
# Default-Stop: | ||
# Short-Description: CookieProxy is used to proxy telegraf connections with cookies | ||
# Description: Telegraf doesn't work well with connecitons that need | ||
# cookies. CookieProxy can work as a shim to proxy through to | ||
# hosts that need cookies. | ||
### END INIT INFO | ||
|
||
|
||
PATH=/bin:/usr/bin:/sbin:/usr/sbin | ||
DESC="cookieproxy" | ||
NAME=cookieproxy | ||
DAEMON=/cookieproxy | ||
PIDFILE=/var/run/cookieproxy.pid | ||
SCRIPTNAME=/etc/init.d/"$NAME" | ||
EXTRA_OPTS="-cookiejar /tmp/cookies/powerwall.txt" | ||
STDOUT=/dev/null | ||
STDERR=/dev/null | ||
|
||
test -f $DAEMON || exit 0 | ||
|
||
. /lib/lsb/init-functions | ||
|
||
case "$1" in | ||
start) log_daemon_msg "Starting Telegraf cookie aware proxy" "cookieproxy" | ||
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON -- $EXTRA_OPTS >>$STDOUT 2>>$STDERR & | ||
log_end_msg $? | ||
;; | ||
stop) log_daemon_msg "Stopping Telegraf cookie aware proxy" "cookieproxy" | ||
killproc -p $PIDFILE $DAEMON | ||
RETVAL=$? | ||
[ $RETVAL -eq 0 ] && [ -e "$PIDFILE" ] && rm -f $PIDFILE | ||
log_end_msg $RETVAL | ||
;; | ||
restart) log_daemon_msg "Restarting Telegraf cookie aware proxy" "cookieproxy" | ||
$0 stop | ||
$0 start | ||
;; | ||
reload|force-reload) log_daemon_msg "Reloading configuration files for Telegraf cookie aware proxy" "cookieproxy" | ||
# there is no real way to reload cookieproxy right now | ||
$0 stop | ||
$0 start | ||
;; | ||
status) | ||
status_of_proc -p $PIDFILE $DAEMON $NAME && exit 0 || exit $? | ||
;; | ||
*) log_action_msg "Usage: /etc/init.d/cookieproxy {start|stop|status|restart|reload|force-reload}" | ||
exit 2 | ||
;; | ||
esac | ||
exit 0 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#!/bin/bash | ||
|
||
# this file supersedes the original entrypoint.sh for telegraf containers | ||
# mainly because we need to fire off a cron job that runs every 20 minutes | ||
|
||
# see: https://github.com/mihailescu2m/powerwall_monitor/issues/14#issuecomment-778478572 | ||
|
||
mkdir -p /tmp/cookies | ||
CURL_CMD="curl -s -k -o /dev/null -c /tmp/cookies/powerwall.txt -X POST -H 'Content-Type: application/json' -d '{\"username\":\"customer\",\"password\":\"$POWERWALL_PASSWORD\",\"force_sm_off\":false}' https://powerwall/api/login/Basic" | ||
CRON_CMD="*/20 * * * * $CURL_CMD" | ||
eval $CURL_CMD | ||
|
||
( crontab -l 2>/dev/null | grep -Fv powerwall ; printf -- "$CRON_CMD\n" ) | crontab | ||
|
||
/etc/init.d/cookieproxy start | ||
|
||
set -e | ||
|
||
if [ "${1:0:1}" = '-' ]; then | ||
set -- telegraf "$@" | ||
fi | ||
|
||
exec "$@" |